Error “The method [] was called on null” in Flutter while parsing Json

↘锁芯ラ 提交于 2020-12-13 04:50:32

问题


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

来源:https://stackoverflow.com/questions/64841541/error-the-method-was-called-on-null-in-flutter-while-parsing-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!