Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restorationScopeId throws OneContext not initiated exception #52

Open
MostafaOsama99 opened this issue Oct 20, 2024 · 1 comment
Open

Comments

@MostafaOsama99
Copy link

Hi, i have this simple app that's using restorationScopeId. as i have figure out after hours of debugging that restorationScopeId causes OneContext not initiated exception

Flutter version: 3.19.0

import 'package:flutter/material.dart';
import 'package:one_context/one_context.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp( const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const Text('hello'),
      restorationScopeId: 'app',
      builder: OneContext().builder,
      navigatorKey: OneContext().key,
    );
  }
}

log

 [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:one_context/src/controllers/one_context.dart': Failed assertion: line 16 pos 12: '_context != null':   OneContext not initiated! please use builder method.
   You need to use the OneContext().builder function to be able to show dialogs and overlays! e.g. ->

   MaterialApp(
     builder: OneContext().builder,
     ...
   )

   If you already set the OneContext().builder early, maybe you are probably trying to use some methods that will only be available after the first MaterialApp build.
   OneContext needs to be initialized by MaterialApp before it can be used in the application. This error exception occurs when OneContext context has not yet loaded and you try to use some method that needs the context, such as the showDialog, dismissSnackBar, showSnackBar, showModalBottomSheet, showBottomSheet or popDialog methods.

   If you need to use any of these OneContext methods before defining the MaterialApp, a safe way is to check if the OneContext context has already been initialized.
   e.g.

   ```dart
     if (OneContext.hasContext) {OneContext().showDialog (...);}

#0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
#1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
#2 OneContext.context (package:one_context/src/controllers/one_context.dart:16:12)
#3 OneContext.hasContext (package:one_context/src/controllers/one_context.dart:20:46)
#4 OneThemeController.loadThemeMode (package:one_context/src/controllers/one_theme_controller.dart:81:20)

@mihailovukojicic
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants