1

I am working on a flutter project where I want is as soon as screen appear a progress bar is shown on the screen until all it gets all the data from database and when I got all the data the progress bar should terminate.

The error I am getting is: This is not the complete error. The error was about too BIG, so I didn't paste the complete error.

E/flutter (  672): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: dependOnInheritedWidgetOfExactType<_LocalizationsScope>() or dependOnInheritedElement() was called before _AllUsersState.initState() completed.
E/flutter (  672): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
E/flutter (  672): Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.
E/flutter (  672): #0      StatefulElement.dependOnInheritedElement.<anonymous closure> (package:flutter/src/widgets/framework.dart:5130:9)
E/flutter (  672): #1      StatefulElement.dependOnInheritedElement (package:flutter/src/widgets/framework.dart:5173:6)
E/flutter (  672): #2      Element.dependOnInheritedWidgetOfExactType (package:flutter/src/widgets/framework.dart:4278:14)
E/flutter (  672): #3      Localizations.of (package:flutter/src/widgets/localizations.dart:472:48)
E/flutter (  672): #4      debugCheckHasMaterialLocalizations.<anonymous closure> (package:flutter/src/material/debug.dart:75:23)
E/flutter (  672): #5      debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:96:4)
E/flutter (  672): #6      showDialog (package:flutter/src/material/dialog.dart:1157:10)
E/flutter (  672): #7      _AllUsersState.getUserData (package:recipedia/Admin/Users/all_users.dart:22:5)
E/flutter (  672): #8      _AllUsersState.initState (package:recipedia/Admin/Users/all_users.dart:82:5)
E/flutter (  672): #9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5015:57)
E/flutter (  672): #10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5)

Below is the Code I wrote.

Future<void> getUserData() async {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        dialogContext = context;
        return ProgressBar(
          message: "Loading..",
        );
      },
    );
    int count = await UserModel().getUsersCount();
    int userID = 101;

    for (int i = 0; i < count - 1; i++, userID++) {
      usersIDs.add(userID.toString());
      usersEmails.add(
          (await UserModel().getUserData(userID.toString(), 'email'))!);
      usersNames.add(
          (await UserModel().getUserData(userID.toString(), 'name'))!);
    }

    List<DataRow> dataRows = [];
    for (int i=0; i<usersEmails.length; i++) {
      dataRows.add(
          DataRow(
              cells: [
                DataCell(Text(
                  textAlign: TextAlign.right
                  ,usersIDs[i],style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.normal
                ),),),
                DataCell(Text(
                  textAlign: TextAlign.left,usersEmails[i],style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.normal
                ),),),
                DataCell(Text(
                  textAlign: TextAlign.left
                  ,usersNames[i],style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.normal
                ),),),
              ]
          ),
      );
    }
    setState(() {
      rows = dataRows;
    });

    if(dialogContext != null) {
      Navigator.pop(dialogContext!);
    }
  }

  @override
  void initState() {
    super.initState();
    getUserData();
  }
Ken White
  • 123,280
  • 14
  • 225
  • 444
Mr Fin
  • 85
  • 8
  • The above link should help you. When you want to call showDialog() directly in initState(), you will need to add an SchedulerBinding.instance.addPostFrameCallback – jraufeisen Dec 01 '22 at 22:34

1 Answers1

0

It happens because your widget is not yet built and you are trying to use context to show dialog over your widget.

Try this solution: https://stackoverflow.com/a/56357477/11943995

This is also a good example: https://stackoverflow.com/a/56401330/11943995