2

Sorry for my very beginner question, I am trying to teach my self Flutter so i am doing some tutorials from youtube and somehow i get Invalid argument(s) error even though i have typed exactly as it is in the tutorial, I tried to debug by printing and first it prints null and then it prints the list with json objects, but when i print mydata[0] it prints The method '[]' was called on null..

import 'package:flutter/material.dart';
import 'dart:convert';

void main() {
  runApp(new MaterialApp(
    home: new Home(),
    theme: ThemeData(
      primarySwatch: Colors.indigo,
    ),
  ));
}

class Home extends StatefulWidget {
  @override
  HomeState createState() => new HomeState();
}

class HomeState extends State<Home> {
  List data;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("Some App"),
      ),
      body: Container(
        child: Center(
          child: FutureBuilder(
            future: DefaultAssetBundle.of(context).loadString('assets/data.json'),
            builder: (context, snapshot) {
              var mydata = json.decode(snapshot.data.toString());
              print(mydata);
              return ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: <Widget>[
                        Text("Name " + mydata[index]['Name']),
                        Text("Age " + mydata[index]['Age']),
                        Text("Gender " + mydata[index]['Gender']),
                      ],
                    ),
                  );
                },
                itemCount: mydata == null ? 0 : mydata.length,
              );
            },
          ),
        ),
      ),
    );
  }
}
2
  • Can you shoe me your data format, so that I can provide a better answer Commented May 19, 2019 at 5:16
  • Can you add the output of print(mydata); ? Commented May 19, 2019 at 8:27

1 Answer 1

6

First of all, don't forget the pubspec.yaml:

  assets:
   - assets/data.json

This is the json:

[
  {
    "Name": "John Doe",
    "Age": "30",
    "Gender": "Male"
  },
  {
    "Name": "Jane Doe",
    "Age": "25",
    "Gender": "Female"
  }
]

You need to check if Future delivered or not inside builder method of FutureBuilder. Like:

This is the build method:

class HomeState extends State<Home> {
  List data;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("Some App"),
      ),
      body: Container(
        child: Center(
          child: FutureBuilder(
            future:
                DefaultAssetBundle.of(context).loadString('assets/data.json'),
            builder: (context, snapshot) {
              if(!snapshot.hasData) { /// CRITICAL POINT
                return CircularProgressIndicator();
              }
              var myData = json.decode(snapshot.data);
              print(myData);
              return ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: <Widget>[
                        Text("Name " + myData[index]['Name']),
                        Text("Age " + myData[index]['Age']),
                        Text("Gender " + myData[index]['Gender']),
                      ],
                    ),
                  );
                },
                itemCount: myData == null ? 0 : myData.length,
              );
            },
          ),
        ),
      ),
    );
  }
}

It's working, but I have better solution for your situation, check it:

Load and read data from Json file

Sign up to request clarification or add additional context in comments.

1 Comment

@SachihiroTakamori I added my other solution, it's a full application code, try it. Don't forget to add file into pubspec.yaml. Also I remember you from Tensor's 2048 video livestream :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.