我是新来的。我试过谷歌搜索,但我无法解决我的问题。我使用“MultiBlocProvider”来管理统计信息。我想改变暗模式状态,如下所示。
ThemeCubit.dart
part 'theme_state.dart';
class ThemeCubit extends HydratedCubit<ThemeState> {
ThemeCubit() : super(ThemeState(AppThemes.lightTheme));
void getTheme(ThemeState state) {
emit(state);
}
@override
ThemeState? fromJson(Map<String, dynamic> json) {
return json['isDark'] as bool
? ThemeState(AppThemes.darkTheme)
: ThemeState(AppThemes.lightTheme);
}
@override
Map<String, bool>? toJson(ThemeState state) {
return {'isDark': state.themeData.brightness == Brightness.dark};
}
}
ThemeState.dart
part of 'theme_cubit.dart';
@immutable
class ThemeState extends Equatable {
final ThemeData themeData;
const ThemeState(this.themeData);
@override
List<Object?> get props => [themeData];
}
主要.dart
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
lazy: true,
create: (context) => ThemeCubit(),
),
],
child:BlocBuilder<ThemeCubit, ThemeState>(
builder: (context,state) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Production Boilerplate',
theme: state.themeData, //ThemeMode.dark,
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
},
),
);
}
}
设置屏幕.dart
Positioned(
top: 60 - widget.offset / 2,
left: 20,
child: Builder(builder: (context) {
return Switch(
value:newValue ,
onChanged: (value) {
BlocProvider.of<ThemeCubit>(context).getTheme(ThemeState(
newValue ? AppThemes.darkTheme : AppThemes.lightTheme));
});
})
),
此代码在使用 "BlocProvider" 时正常工作。但是当我使用“MultiBlocProvider”时,出现以下错误。
以下断言附加到渲染树:'package:flutter/src/widgets/framework.dart':断言失败:第 4357 行 pos 14:'owner!._debugCurrentBuildTarget == this':不正确。
断言表明框架本身存在错误,或者我们应该在此错误消息中提供更多信息,以帮助您确定和修复根本原因。无论哪种情况,请通过在 GitHub 上提交错误报告此断言:
https ://github.com/flutter/flutter/issues/new?template=2_bug.md抛出异常时,这是堆栈:#2 Element.rebuild。(package:flutter/src/widgets/framework.dart:4357:14) #3 Element.rebuild (package:flutter/src/widgets/framework.dart:4360:6) #4 ComponentElement._firstBuild (package:flutter/src /widgets/framework.dart:4643:5) #5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4638:5) #6 Element.inflateWidget (package:flutter/src/widgets/framework.dart :3673:14) #7 Element.updateChild (package:flutter/src/widgets/framework.dart:3425:18) #8 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16) # 9 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5) #10 RenderObjectToWidgetAdapter.attachToRenderTree。(包:flutter/src/widgets/binding.dart:1112:18)#11 BuildOwner。buildScope (package:flutter/src/widgets/framework.dart:2573:19) #12 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13) #13 WidgetsBinding.attachRootWidget (package:flutter/ src/widgets/binding.dart:944:7) #14 WidgetsBinding.scheduleAttachRootWidget。(包:flutter/src/widgets/binding.dart:924:7)(从 _AssertionError 类、_RawReceivePortImpl 类、_Timer 类、dart:async 和 dart:async-patch 中删除了 13 帧)
我该如何解决?