emanuel-braz / one_context

pub: https://pub.dev/packages/one_context - OneContext provides a simple way to deal with Dialogs, Overlays and Navigations with no need of BuildContext.
BSD 3-Clause "New" or "Revised" License
80 stars 24 forks source link

restorationScopeId throws OneContext not initiated exception #52

Open MostafaOsama99 opened 1 week ago

MostafaOsama99 commented 1 week ago

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 commented 1 week ago

+1