Nada Khaled
Nada Khaled

Reputation: 189

flutter and json : NoSuchMethodError: The method '[]' was called on null

hello I'm making a new flutter app and I'm new to flutter , I'm using a mysql database and iam getting the response as json . Here is my formatted json response :

JsonResponse

{
   "folders":[
      {
         "id":2107,
         "name":"Labs ",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-09-30 03:00:39",
         "inarchive":0,
         "active":1
      },
      {
         "id":2108,
         "name":"L1",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-09-30 03:00:45",
         "inarchive":0,
         "active":1
      },
      {
         "id":2130,
         "name":"L2",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-02
02:39:11",
         "inarchive":0,
         "active":1
      },
      {
         "id":2175,
         "name":"L3",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-10-07 03:43:15",
         "inarchive":0,
         "active":1
      },
      {
         "id":2202,
         "name":"L4",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-09 02:08:10",
         "inarchive":0,
         "active":1
      },
      {
         "id":2227,
         "name":"\u0645\u0631\u0627\u062c\u0639\u0627\u062a
",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-10-11 05:15:43",
         "inarchive":0,
         "active":1
      },
      {
         "id":2242,
         "name":"L5",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-14 03:45:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2257,
         "name":"L6",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-16
06:23:41",
         "inarchive":0,
         "active":1
      },
      {
         "id":2269,
         "name":"L7",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-21 03:39:12",
         "inarchive":0,
         "active":1
      },
      {
         "id":2309,
         "name":"L8",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-23 05:14:48",
         "inarchive":0,
         "active":1
      },
      {
         "id":2310,
         "name":"L8",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-23
05:14:51",
         "inarchive":0,
         "active":1
      },
      {
         "id":2346,
         "name":"L9",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-28 04:22:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2370,
         "name":"L10",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-10-30 03:02:00",
         "inarchive":0,
         "active":1
      },
      {
         "id":2446,
         "name":"L11",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-12
06:28:13",
         "inarchive":0,
         "active":1
      },
      {
         "id":2462,
         "name":"\u062a\u0628\u064a\u0636\u0627\u062a",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-11-13 08:39:00",
         "inarchive":0,
         "active":1
      },
      {
         "id":2464,
         "name":"L12",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-13
08:40:55",
         "inarchive":0,
         "active":1
      },
      {
         "id":2488,
         "name":"L13",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18 06:09:11",
         "inarchive":0,
         "active":1
      },
      {
         "id":2490,
         "name":"L14",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18 06:38:59",
         "inarchive":0,
         "active":1
      },
      {
         "id":2491,
         "name":"L15",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-18
06:45:57",
         "inarchive":0,
         "active":1
      },
      {
         "id":2497,
         "name":"\u062a\u0644\u062e\u064a\u0635\u0627\u062a",
         "img":null,
         "haschild":1,
         "parentid":1955,
         "createdate":"2019-11-19 04:24:49",
         "inarchive":0,
         "active":1
      },
      {
         "id":2509,
         "name":"L16",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-20
09:56:04",
         "inarchive":0,
         "active":1
      },
      {
         "id":2522,
         "name":"L17",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-25 06:38:27",
         "inarchive":0,
         "active":1
      },
      {
         "id":2542,
         "name":"L18",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-27 12:19:12",
         "inarchive":0,
         "active":1
      },
      {
         "id":2552,
         "name":"Protein
Lab",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-27 16:32:27",
         "inarchive":0,
         "active":1
      },
      {
         "id":2562,
         "name":"Chromatography ",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-11-30 15:24:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2576,
         "name":"L19",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-02
02:05:29",
         "inarchive":0,
         "active":1
      },
      {
         "id":2580,
         "name":"L20",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-02 08:23:48",
         "inarchive":0,
         "active":1
      },
      {
         "id":2592,
         "name":"21",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-03 06:27:24",
         "inarchive":0,
         "active":1
      },
      {
         "id":2600,
         "name":"L22",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-04
06:22:46",
         "inarchive":0,
         "active":1
      },
      {
         "id":2614,
         "name":"L23",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-04 10:03:00",
         "inarchive":0,
         "active":1
      },
      {
         "id":2640,
         "name":"L24",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-08 06:53:11",
         "inarchive":0,
         "active":1
      },
      {
         "id":2644,
         "name":"L25",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-09
05:33:07",
         "inarchive":0,
         "active":1
      },
      {
         "id":2649,
         "name":"L26",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-09 07:58:42",
         "inarchive":0,
         "active":1
      },
      {
         "id":2711,
         "name":"\u0623\u0633\u0626\u0644\u0629",
         "img":null,
         "haschild":0,
         "parentid":1955,
         "createdate":"2019-12-19
05:18:11",
         "inarchive":0,
         "active":1
      }
   ],
   "files":[
      {
         "id":4542,
         "name":"Ch2-part1.m4a 27.52MB",
         "img":null,
         "uploader":"Aws",
         "url":"http:\/\/msc-mu.com\/..\/uploaded\/031020190Ch2-part1.m4a",
         "createdate":"2019-10-03 09:53:39",
         "approved":1,
         "active":1
      }
   ]
}

And whenever I try to implement the list of folders and files it says that there is nothing inside the list and its null here is my code : fellow.dart


class SecondScreen extends StatefulWidget {

  final YearsMain value;

  SecondScreen({Key key, this.value}) : super(key: key);

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

class _SecondScreenState extends State<SecondScreen> {
  List<Folders> list= <Folders>[];
  Future<List<Folders>> getData() async {
    String link = "http://msc-mu.com/api_verfication.php";
    try {
      var res = await http.post(link,
          body: {
            'flag': 'selectfellowsubjects',
            'parentsubject': widget.value.id
          });
      print(res.body);
      if (res.statusCode == 200) {
        var data = json.decode(res.body);
        var rest = data["folders"].forEach((v) {
          list.add(Folders.fromJson(v));
        });
        print(rest);
      }
      return list;
    } catch (e) {
      debugPrint('$e');
    }
  }

  Widget listViewWidget(List<Folders> article) {
    return Container(
      child: ListView.builder(
          itemCount: 20,
          padding: const EdgeInsets.all(2.0),
          itemBuilder: (context, position) {
            return Card(
              child: ListTile(
                title: Text(
                  '${article[position].name}',
                  style: TextStyle(
                      fontSize: 18.0,
                      color: Colors.black,
                      fontWeight: FontWeight.bold),
                ),
                onTap: () => _onTapItem(context, article[position]),
              ),
            );
          }),
    );
  }

  void _onTapItem(BuildContext context, Folders folders) {
    Navigator.of(context).push(MaterialPageRoute(
        builder: (BuildContext context) => DownloadPage()));
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print(widget.value.id);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:  Center(
        child: new FutureBuilder<List<Folders>>(
          future: getData(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              List fellow = snapshot.data;
              return listViewWidget(fellow);
            } else if (snapshot.hasError) {
              return Text('${snapshot.error}');
            }
            return CircularProgressIndicator();
          },
        ),

      ),
    );
  }
}

the function did get the "folders" list but this is the output :

I/flutter (32601): NoSuchMethodError: The method '[]' was called on null.
I/flutter (32601): Receiver: null
I/flutter (32601): Tried calling: []("id")

and this is my Folders.class

class Folders {
   String id;
   String name;

  Folders({this.id, this.name});

  factory Folders.fromJson(Map<String, dynamic> json)=> Folders(id: json["folders"]["id"].toString(),name: json["folders"]["name"]);
}

am I doing anything wrong ?

Upvotes: 0

Views: 293

Answers (1)

David Sedl&#225;ř
David Sedl&#225;ř

Reputation: 777

Remove ['folders'] in

 factory Folders.fromJson(Map<String, dynamic> json)=> Folders(id: json["folders"]["id"].toString(),name: json["folders"]["name"]);

You are already browsing folders in data["folders"].forEach and passing each object into constructor. Therefore no individual object has 'folders' property.

      {
     "id":2108,
     "name":"L1",
     "img":null,
     "haschild":0,
     "parentid":1955,
     "createdate":"2019-09-30 03:00:45",
     "inarchive":0,
     "active":1
  },

Upvotes: 0

Related Questions