OpenFlutter / flutter_screenutil

Flutter screen adaptation, font adaptation, get screen information
https://pub.dartlang.org/packages/flutter_screenutil
Apache License 2.0
3.88k stars 494 forks source link

Looking up a deactivated widget's ancestor is unsafe #367

Closed rgtstha closed 2 years ago

rgtstha commented 2 years ago

I am getting an exception when the screen changes using BottomNavigationBar.

Here is the full error

FlutterError (Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.)

Material App

return ScreenUtilInit(
      designSize: const Size(375, 812),
      builder: (_) => MaterialApp(
          title: 'Flutter Demo',
          useInheritedMediaQuery: true,
          theme: AppTheme.theme(context),
          home: const OnboardingScreen1(),
          builder: (context, widget) {
            ScreenUtil.setContext(context);
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: widget!,
            );
          }),
    );

Error screen


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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RefreshIndicator(
        onRefresh: () async {
          // debugPrint("Refreshing");
          // await Future.delayed(const Duration(seconds: 1));
        },
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: AppSizes.globalPadding.w),
          child: CustomScrollView(
            slivers: [
              // Text Field for search
              SliverToBoxAdapter(
                child: Container(
                  margin: EdgeInsets.symmetric(vertical: 20.h),
                  child: TextField(
                    decoration: InputDecoration(
                      contentPadding: EdgeInsets.fromLTRB(
                        AppSizes.dimen_14.w,
                        AppSizes.dimen_14.h,
                        AppSizes.dimen_14.w,
                        AppSizes.dimen_14.h,
                      ),
                      prefixIcon: const Icon(Icons.search),
                      hintText: "Search audio/speaker/sessions",
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(100.r),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Error Screen Shot 2022-04-15 at 10 52 46

I some how fixed the issue by setting ScreenUtil.setContext(context); in each screen. Is there any better approach ?

Mounir-Bouaiche commented 2 years ago

Please use the context that ScreenUtilInit's builder provide.

ScreenUtilInit(
  ...,
  builder: (ctx) => MaterialApp(
    ...,
    theme: AppTheme.theme(ctx),
    ...,
  ),
  ...,
),

And make sure you don't use any ScreenUtil.setContext() in the app since you use ScreenUtilInit() or else you must use ScreenUtil.setContext() on any other screen that uses the library, at least if it's not a descendent.