1

I was creating a Flutter application which parses a simple JSON, and it works well but gives an error in Debug Console in VS Code,

My simple JSON file:

json file

With Lightest Code snippet :

import 'package:flutter/material.dart'; import 'dart:convert'; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp(title: "J2", home: new Homepage()); } } class Homepage extends StatefulWidget { @override State createState() => new HomepageState(); } class HomepageState extends State<Homepage> { @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar(title: new Text("J2")), body: new Container( child: new FutureBuilder( future: DefaultAssetBundle.of(context) .loadString('assets/data.json'), builder: (context, snapshot) { Map<String, dynamic> mydata = json.decode(snapshot.data.toString()); return new Container(child: new Text(mydata['city'])); }))); } } 

And it works well but with this error in the debug console, how can I overcome it? I am a beginner.

Error Message

6
  • Welcome to so. You need to copy your code. The screenshot is not proper. Commented Nov 15, 2020 at 5:48
  • 1
    Does this answer your question? Flutter: get default context? or load assets without context? Commented Nov 15, 2020 at 5:54
  • I think you can help me now. Commented Nov 15, 2020 at 6:51
  • The error message saying that your myData is null and myData['city'] is calling on a null object. Commented Nov 15, 2020 at 7:22
  • Thanks, the same is written is there but my question is why am I getting an error if I have data in mydata. Please check the JSON file also attached there. Commented Nov 15, 2020 at 7:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.