-1

I am getting this error after using JSON data into text wigdet. My json is stored locally in

assets/json/data.json

I am building a Listview using ListView.builder

My code

 Widget build(BuildContext context) {
  return Scaffold(
    backgroundColor: Color(0xff655ee6),
    appBar: AppBar(
      backgroundColor: Color(0xff655ee6),
      title: Text("Apply Online"),
    ),
    body: SingleChildScrollView(
      child: SizedBox(
        height: MediaQuery.of(context).size.height,
        child: FutureBuilder(
          future: DefaultAssetBundle.of(context).loadString("assets/json/example.json"),
          builder: (context,snapshot){
            var mydata = json.decode(snapshot.data.toString());
            if(mydata == null){
              return Center(
                child: CircularProgressIndicator(
                ),
              );
            }
            else {return ListView.builder(
              itemCount: 30,
              itemBuilder: (context, index){
                return Card(
                  child: ListTile(
                    title: Text(mydata["saarthiService"]['applyOnline']["0"]),
                    leading: Icon(Icons.import_contacts),

                  ),
                );
              },
            );}
          },
        ),
      ),
    ),
    );
 }
} 

Please Help Me and I am new to flutter Thanks

codingsenpi
  • 71
  • 1
  • 6

2 Answers2

2

This error occurs when you try to access any value from a map using key or from a list using index but your map/list is null.
In your case this line:

mydata["saarthiService"]['applyOnline']["0"]

is most probably causing the error. Make sure:

  • mydata is not null
  • mydata["saarthiService"] is not null
  • mydata["saarthiService"]['applyOnline'] is not null

I will advice you to check your data by printing to the console and verify that it is not null.

Md Azharuddin
  • 1,346
  • 1
  • 9
  • 30
0

error is coming from this line

mydata["saarthiService"]['applyOnline']["0"]

some of the map from nested Map is null

to remove the error just use null operater and give a empty map to that.

 ((mydata["saarthiService"]?? {})['applyOnline'] ?? {})["0"] ?? " 

the ?? operater checks if the map is null if the map is null then we create an empty map and next key will be accessed from this

just use this your error will be solved

rishabh mistry
  • 527
  • 3
  • 8
  • This is simply incorrect. This will still result in an error. – Christopher Moore Oct 16 '20 at 13:10
  • have you checked. it worked for me – rishabh mistry Oct 16 '20 at 13:15
  • You don't know the data the OP has. It's impossible to test so I very much doubt you know it works for sure. Based on my knowledge of the error, this will not solve it. Please see the linked duplicate. – Christopher Moore Oct 16 '20 at 13:24
  • based on my experience it worked for me all the time. just check and let me know – rishabh mistry Oct 16 '20 at 13:27
  • Why would this work? The error is a result of this line `mydata["saarthiService"]['applyOnline']["0"]`. SImply running this line of code will result in an error. Your code adds a null-check **after** this line of code is run. Therefore, the error will still occur and it will happen before it reaches your added code. – Christopher Moore Oct 16 '20 at 13:31
  • the null saftey operater is for that as soon as line throws called on null Exception it checks for null saftey and sets the Default value if present. just read documentation and try by your self it will work – rishabh mistry Oct 16 '20 at 13:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223161/discussion-between-christopher-moore-and-rishabh-mistry). – Christopher Moore Oct 16 '20 at 13:38
  • @ChristopherMoore i have updated the answer it will work 100% – rishabh mistry Oct 16 '20 at 14:06
  • Yes at the very least this *new* code will stop the error. – Christopher Moore Oct 16 '20 at 14:11
  • I will try the solution as soon as My pc will be accessible. And I will update the question with more information. Thanks everyone – codingsenpi Oct 17 '20 at 02:14