Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

Flutter showing The method '[]' was called on null. on Listbuilder

I am fetching data from an api and showing in ListView builder But its showing error

 class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  bool show = false;
  var map = {};
  @override
  void initState() {
    dosomestuff();

  }
  Future<List> dosomestuff() async {
    http.Response res = await http.get(
      'http://retailapi.airtechsolutions.pk/api/menu/2112',
    );

    Map<String, dynamic> map = json.decode(res.body);

    if (map['description'] == "Success") {
      print('show kr ');
      print(map["Categories"].length);
      print(map["Categories"]);
      setState(() {
        show = true;
      });
    }
  }



  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text(widget.title),
      ),
      body: show
          ? ListView.builder(
        itemCount: map["Categories"].length,
        scrollDirection: Axis.horizontal,
        shrinkWrap: true,
        physics: BouncingScrollPhysics(),
        itemBuilder: (context, index) {
          return Container(
            width: 80.0,
            child: Text(map["Categories"][index]['Name'])
          );
        },
      ) : Container(),

    );
  }
}

Its showing error The method '[]' was called on null. But when i print the values in state function the its priting values and all things are working but when ill show in ListBuilder then its showing that error.

You can see i print the values in function

  print(map["Categories"].length);
  print(map["Categories"]);

And its printing values all things fine.

enter image description here

You can see in image its printing the length and arrays fine so its not null when i use in widget its saying length is called on null -_-

question from:https://stackoverflow.com/questions/65830268/flutter-showing-the-method-was-called-on-null-on-listbuilder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

can you try this I might have mistyped the parentheses.

class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      bool show = false;
      var map = {};
      List<dynamic>  getCategory;

   @override
    void initState() {
     dosomestuff();

      }
 Future<List> dosomestuff() async {
       await http.get(
       'http://retailapi.airtechsolutions.pk/api/menu/2112',
        ).then((result){
       http.Response res=result;
       Map<String, dynamic> map = json.decode(res.body);

       if (map['description'] == "Success") {       
      if(map["Categories"].length>0){
         setState(() {
           show = true;
      getCategory=map["Categories"];
        });
        }
     }
   }

   });






 @override
 Widget build(BuildContext context) {

  return Scaffold(
    appBar: AppBar(

     title: Text(widget.title),
    ),
   body: show
      ? ListView.builder(
    itemCount: getCategory.length,
    scrollDirection: Axis.horizontal,
    shrinkWrap: true,
    physics: BouncingScrollPhysics(),
    itemBuilder: (context, index) {
      return Container(
        width: 80.0,
        child: Text(getCategory[index]['Name'])
      );
    },
    ) : Container(),

    );
   }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...