1

I am trying to create a quizapp in which i create cards, like when user click on specific card then it will be redirect to that quizpage.

here is the where i am trying to get json file data which are quiz questions

class getjson extends StatelessWidget {
 
  @override
  Widget build(BuildContext context) {
    
    return FutureBuilder(
      future:DefaultAssetBundle.of(context).loadString("assets/questions/generalQ.json"),
        builder: (context, snapshot) {
        List mydata = json.decode(snapshot.data.toString());
        print(mydata);
        if (mydata == null) {
          return Scaffold(
            body: Center(
              child: Text(
                "Loading",
              ),
            ),
          );
        } else {
          return quizpage();
        }
      },
    );
  }
}


class quizpage extends StatefulWidget {
  @override
  _quizpageState createState() => _quizpageState();
}

class _quizpageState extends State<quizpage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}

and here i created cards widgets

Widget customcard(String langname, String image, String des){
    return Padding(
      padding: EdgeInsets.symmetric(
        vertical: 20.0,
        horizontal: 30.0,
      ),
      child: InkWell(
        onTap: (){
          Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context)=>getjson(),));
        },

i am calling getjson class on the onTap.

when i run the app click on card it shows me "loading", it is returning null on "mydata" of getjson class.

how to fix it? why it's returning null on mydata? Please help!

JSON FILE:

[
    {
        "1": "What is the world's most populated country?",
        "2": "Each year World Red Cross and Red Crescent Day is celebrated on",
        "3": "The ozone layer restricts"
    },
    {
        "1": {
            "a": "Russia",
            "b": "China",
            "c": "USA"
        },
        "2": {
            "a": "May 8",
            "b": "May 18",
            "c": "April 28"
           
        },
        "3": {
            "a": "Visible light",
            "b": "Ultraviolet radiation",
            "c": "Infrared radiation"
        }
    }
]

TimeToCode
  • 1,458
  • 3
  • 18
  • 60

1 Answers1

0

Actually it's pretty simple, you say that your list = a Future . When you do json.decode(snapshot.data) the Future of your futureBuilder is not yet finished, so mydata at that time is snapshot.data which is null. You should write:

if(snapshot.ConnectionState == ConnectionState.done){
   List mydata = json.decode(snapshot.data.toString());
   return quizpage();

}else{
return Scaffold(
           body: Center(
             child: Text(
               "Loading",
             ),
           ),
         );
}
user14624595
  • 874
  • 7
  • 19
  • i have added json file, can u please check . – TimeToCode May 06 '21 at 07:44
  • but i still get null on data of snapshot. – TimeToCode May 06 '21 at 08:26
  • and it also print this error," The method '[]' was called on null. Receiver: null Tried calling: [](0)" – TimeToCode May 06 '21 at 08:27
  • Do you pass your mydata List on the quizpage after that? Did the loading change to your quizapp container? Are you sure the json you are trying to read is in the correct directory? Plus it is a good practice to define what you will get from json. In your json you return a List>, so in order to call mydata you should write something like `mydata[0]["1"]` .What is it that you call [](0)? mydata[0] is a Map to call a map with a key value pair you should write map["key"]. – user14624595 May 06 '21 at 08:38
  • when i run the app this error came up, and json file is in correct directory, and i am returning mydata like this return quizpage(mydata:mydata); – TimeToCode May 06 '21 at 08:50
  • please print(mydata) before returning quizpage, and show me how you get the elements from that list is it as stated above? – user14624595 May 06 '21 at 08:53
  • i just return return quizpage(); now it's showing blank screen after clicking on card, blank screen is just because i have not did anything on quizpage , but i'm printing mydata and it is null – TimeToCode May 06 '21 at 09:00
  • If you print mydata after connectionState == ConnectionState.done and it is still null, I fail to see the solution sorry. – user14624595 May 06 '21 at 09:48
  • i print it before returning quizpage(). Ok, thank you for your time. – TimeToCode May 06 '21 at 10:17