Flutter - The getter 'length' was called on null
Andrew Mclaughlin
I am trying to develop a flutter app. This flutter is creating teams for a card game. After the creation of the team, the points could be counted through the, so that you don't have to think about how many points everybody has.
But I got an exception, where I know where the exception and what it means, but i do not have any clue how i could solve the problem. I hope some of you guys could help me.
This is the code where the error is thrown:
import 'package:flutter/material.dart';
class Punktezaehler extends StatefulWidget{ final List<String> spieler_namen; Punktezaehler(this.spieler_namen); @override State<StatefulWidget> createState() => new _Punktezaehler(this.spieler_namen);
}
class _Punktezaehler extends State<Punktezaehler>{ final List<String> spieler_namen; _Punktezaehler(this.spieler_namen); List<int> punkteanzahl_teamEins; List<int> punkteanzahl_teamZwei; @override Widget build(BuildContext context) { var spieler1 = spieler_namen[0].substring(0,3); var spieler2 = spieler_namen[1].substring(0,3); var spieler3 = spieler_namen[2].substring(0,3); var spieler4 = spieler_namen[3].substring(0,3); return new Scaffold( appBar: new AppBar( automaticallyImplyLeading: false, title: new Text("$spieler1 & $spieler2 vs" +" $spieler3 & $spieler4"), actions: <Widget>[ ], ), body: Container( child: new Row( children: <Widget>[ new Column( children: <Widget>[ new IconButton( icon: Icon(Icons.exposure_plus_2), onPressed: () => punkte_hinzuzaehlen(1, 2) ) ], ), new Column( children: <Widget>[ //new FlatButton(onPressed: () => print(punkteanzahl_teamEins.length), child: new Text("Punkte")), ListView.builder( itemCount: punkteanzahl_teamEins.length, //--> Error is thrown here itemBuilder: (context, index){ return Text(punkteanzahl_teamEins[index].toString()); } ), new Row() ], ), new Column( children: <Widget>[ new IconButton( icon: Icon(Icons.exposure_plus_2), onPressed: () => punkte_hinzuzaehlen(2, 2) )], ) ], ) ), ); } void punkte_hinzuzaehlen(int team, int nummer){ if (team == 1){ //Team 1 bekommt die Punkte print("Team 1 gets points"); } else if(team == 2){ //Team 2 bekommt die Punkte print("Team 2 gets points"); } }
}And this is the error message:
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (26028): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (26028): The getter 'length' was called on null.
I/flutter (26028): Receiver: null
I/flutter (26028): Tried calling: lengthAfter the fix, I got another error:
══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (26028): The following assertion was thrown during performResize():
I/flutter (26028): Vertical viewport was given unbounded width.
I/flutter (26028): Viewports expand in the cross axis to fill their container and constrain their children to match
I/flutter (26028): their extent in the cross axis. In this case, a vertical viewport was given an unlimited amount of
I/flutter (26028): horizontal space in which to expand. 1 6 Answers
punkteanzahl_teamEins is only declared. But not initialized. So it is throwing null error.
You should assign value to punkteanzahl_teamEins as
List<int> punkteanzahl_teamEins = [1,4,5,7];or pass data from parent as requirement.
4We spend lost's of time to resolve issue finally we got a solution is: Please check you pubspec.yaml and remove extra '-' in case of assets file and also please follow the structure of that like spacing and all that.
We are must sure that issue only in pubspec.yaml file
0First one check spieler_namen is null or not.If it is null then use below code i hope this will solve your problem.....
if(spieler_namen == null){ new Container(width: 10.0,height: 10.0,); }else{ your requirement ....... } In our case, we got this error when Internet Connection is off after calling webservice.
1Initialize your list, and run hot restart (press R).
It works correctly.
I fix this by revisiting this section in pubspec.yaml
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpegEnsure your spacing, hyphens and filenames are correct. Note: You do not need to list every image file; instead, you can just list their directory:
assets:
- images/