先強調一下,很多問題可以使用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)),
);
}