flutter flutter_screenutil Looking up a deactivated widget's ancestor is unsafe.


先強調一下,很多問題可以使用reStart更新試一下下!!!!!

使用flutter_screenutil 報錯 Looking up a deactivated widget's ancestor is unsafe.

The following assertion was thrown while dispatching notifications for SwiperController:
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.

為SwipController發送通知時引發了以下斷言:

查找停用小部件的祖先是不安全的。

此時,小部件元素樹的狀態不再穩定。

要在dispose()方法中安全地引用小部件的祖先,請保存對祖先的引用

通過

在小部件的didChangeDependencies()方法中調用dependOnInheritedWidgetOfExactType()。

初始代碼,這個組件引用到了ListView里,開始報錯

在這里插入圖片描述
在這里插入圖片描述

經過仔細閱讀使用文檔,核對版本號

新版本代碼如下

首先要在lib/main.dart注冊一下

在這里插入圖片描述

import 'package:flutter/material.dart';
import 'pages/tabs/Tabs.dart';
// import 'routers/router.dart';
import 'pages/tabs/Tabs.dart';
import 'pages/test/Search.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

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

// 輸入stf  有狀態組件
// stss 無狀態組件
class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  final Map<String, WidgetBuilder> routes = {
    '/': (context) => Tabs(),
    '/search': (context) => SearchPage(),
  };

  Route<dynamic> _onGenerateRoute(RouteSettings settings) {
    //   return MaterialPageRoute(builder: (context) {
    // 如果訪問的路由頁需要登錄,但當前未登錄,則直接返回登錄頁路由,
    // 引導用戶登錄;其它情況則正常打開路由。
    // 統一處理
    // })
    final String? name = settings.name;
    final Function pageContentBuilder = routes[name] as Function;
    print('到1');
    if (settings.arguments != null) {
      print('到2');
      final Route route = MaterialPageRoute(
          builder: (context) =>
              pageContentBuilder(context, arguments: settings.arguments));
      return route;
    } else {
      print('到3');
      final Route route =
          MaterialPageRoute(builder: (context) => pageContentBuilder(context));
      return route;
    }
  }

  @override
  Widget build(BuildContext context) {
    // return MaterialApp(
    //   // home: Tabs()
    //   initialRoute: '/',
    //   onGenerateRoute: _onGenerateRoute,
    // );
    return ScreenUtilInit(
      designSize: Size(360, 690),
      builder: () => MaterialApp(
        // home: Tabs()
        initialRoute: '/',
        onGenerateRoute: _onGenerateRoute,
      ),
    );
  }
}

使用案例

Widget _titleWidget(value) {
    return Container(
      height: 32.h,
      margin: EdgeInsets.only(left: 20.w),
      padding: EdgeInsets.only(left: 20.w),
      decoration: BoxDecoration(
          border: Border(left: BorderSide(color: Colors.red, width: 10.w))),
      child: Text(value, style: TextStyle(color: Colors.pink)),
    );
  }

最需要注意的一點點!!!!使用reStart更新試一下下!!!!!

在這里插入圖片描述

大功告成

在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM