[Solved] How to load json in main method Flutter?


Found the Solution using FutureBuilder

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {


  _load() async {
    final file = await rootBundle.loadString('assets/j.json');
    final data = await jsonDecode(file);
    print(data.toString());
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: _load(),
        builder: (_, AsyncSnapshot<dynamic> snapshot) {
          return !snapshot.hasData
              ? Container(
                  color: Colors.white,
                  child: Center(
                      child: Container(
                    child: CircularProgressIndicator(),
                    width: 20,
                    height: 20,
                  )),
                )
              : MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        appBarTheme: AppBarTheme(
          titleTextStyle: Theme.of(context).textTheme.headline1,
        ),
      ),
      home: Scaffold(
        body: Text("Body"),

        // MaxWidthButton(),
      ),
    );
        });
  }
}

1

solved How to load json in main method Flutter?