Skip to main content
added 7 characters in body
Source Link
lava
  • 7.6k
  • 2
  • 44
  • 35

If you didn't add the correct location catalog.json in pubsec.yaml your variable catalog didn't gets the correct value so the late variable is not initialized.

If you didn't add the correct location catalog.json in pubsec.yaml your variable catalog gets the correct value so the late variable is not initialized.

If you didn't add the correct location catalog.json in pubsec.yaml your variable catalog didn't gets the correct value so the late variable is not initialized.

added 3871 characters in body
Source Link
lava
  • 7.6k
  • 2
  • 44
  • 35

So you must add asset path in pubsec.yaml

  assets:
    - assets/
    - assets/images/files/catalog.json
  

Another case here is JSON.decode() return map<string,dynamic> value here you set list.maybe that also cause the problem and not initialised.

enter image description here

instead of this late List catalogdata; use this late var catalogdata; or late Map<string,dynamic> catalogdata;

Sample Code

Catalog.json

{
  "name": "lava",
  "Catagory": "man"
}

Main.dart

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class AppRoutes {
  static String detail = "/Detail";
  static String Page2 = "/FilterBeacon";
  static String Page1 = "/FilterPoint";
  static String home = "/";
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // key: constItem.navigatorKey,

      initialRoute: "/",
      routes: {
        AppRoutes.home: (context) => Home(),
      },
      title: _title,
      // home: ,
    );
  }
}

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

var _index = 0;

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Homepage(),
    );
  }
}

class Homepage extends StatefulWidget {
  const Homepage({Key? key}) : super(key: key);

  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  late var catalogdata;

  Future<String> loadDatas() async {
    var data = await rootBundle.loadString("assets/images/files/catalog.json");
    // setState(() {
      catalogdata = json.decode(data);
    // });
    return "success";
  }

  Future<String> loadData() async {
    var data = await rootBundle.loadString("assets/images/files/catalog.json");
    setState(() {
    catalogdata = json.decode(data);
    });
    return "success";
  }

  @override
  void initState() {
    loadData();
    // loadData().then((value) => catalogdata=value);
  } // String jsons = "";

  // @override
  // Future<void> initState() async {
  //   super.initState();
  //   await loadData();
  // }

  @override
  Widget build(BuildContext context) {
    var futureBuilder = FutureBuilder(
          future:  loadData(),
          builder: (context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return Center(child: CircularProgressIndicator());
            else if (snapshot.connectionState == ConnectionState.done)
              return Center(
                child: Text(
                  catalogdata.toString(),
                  style: TextStyle(),
                ),
              );
            else
              return Container();
          });
    return Scaffold(
      appBar: AppBar(
        title: Text("Homepage"),
      ),
      body: Center(
        child: Text(
          catalogdata.toString(),
          style: TextStyle(),
        ),
      ),
    );
  }
}

Another case here is JSON.decode() return map<string,dynamic> value here you set list.maybe that also cause the problem and not initialised.

enter image description here

So you must add asset path in pubsec.yaml

  assets:
    - assets/
    - assets/images/files/catalog.json
  

Another case here is JSON.decode() return map<string,dynamic> value here you set list.maybe that also cause the problem and not initialised.

enter image description here

instead of this late List catalogdata; use this late var catalogdata; or late Map<string,dynamic> catalogdata;

Sample Code

Catalog.json

{
  "name": "lava",
  "Catagory": "man"
}

Main.dart

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class AppRoutes {
  static String detail = "/Detail";
  static String Page2 = "/FilterBeacon";
  static String Page1 = "/FilterPoint";
  static String home = "/";
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // key: constItem.navigatorKey,

      initialRoute: "/",
      routes: {
        AppRoutes.home: (context) => Home(),
      },
      title: _title,
      // home: ,
    );
  }
}

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

var _index = 0;

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Homepage(),
    );
  }
}

class Homepage extends StatefulWidget {
  const Homepage({Key? key}) : super(key: key);

  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  late var catalogdata;

  Future<String> loadDatas() async {
    var data = await rootBundle.loadString("assets/images/files/catalog.json");
    // setState(() {
      catalogdata = json.decode(data);
    // });
    return "success";
  }

  Future<String> loadData() async {
    var data = await rootBundle.loadString("assets/images/files/catalog.json");
    setState(() {
    catalogdata = json.decode(data);
    });
    return "success";
  }

  @override
  void initState() {
    loadData();
    // loadData().then((value) => catalogdata=value);
  } // String jsons = "";

  // @override
  // Future<void> initState() async {
  //   super.initState();
  //   await loadData();
  // }

  @override
  Widget build(BuildContext context) {
    var futureBuilder = FutureBuilder(
          future:  loadData(),
          builder: (context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return Center(child: CircularProgressIndicator());
            else if (snapshot.connectionState == ConnectionState.done)
              return Center(
                child: Text(
                  catalogdata.toString(),
                  style: TextStyle(),
                ),
              );
            else
              return Container();
          });
    return Scaffold(
      appBar: AppBar(
        title: Text("Homepage"),
      ),
      body: Center(
        child: Text(
          catalogdata.toString(),
          style: TextStyle(),
        ),
      ),
    );
  }
}
Source Link
lava
  • 7.6k
  • 2
  • 44
  • 35

it's showing this error "lateinitializationError: Field 'catalogdata' has not been initialized." then I tried by initializing the 'catalogdata'

While using late before variables make sure that, the variable must be initialized later. Otherwise, you can encounter a runtime error when the variable is used.

If you didn't add the correct location catalog.json in pubsec.yaml your variable catalog gets the correct value so the late variable is not initialized.

enter image description here

Another case here is JSON.decode() return map<string,dynamic> value here you set list.maybe that also cause the problem and not initialised.

enter image description here