以下是“我的应用程序”屏幕:
import 'package:api_call_sample/Repository/JokeRepository.dart';
import 'package:api_call_sample/bloc/jokeevent.dart';
import 'package:api_call_sample/bloc/jokestate.dart';
import 'package:flutter/material.dart';
import "package:api_call_sample/bloc/jokebloc.dart";
import 'package:flutter_bloc/flutter_bloc.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocProvider(
create: (context) => JokeBloc(
RepositoryProvider.of<JokeRepository>(context),
)..add(LoadJokeEvent()),
child: const HomeScreen()),
);
}
}
这是主屏幕屏幕:在Bloc Builder屏幕中出现错误
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Joke Api"),
),
**body: Column( children: [**
**BlocBuilder<JokeBloc, JokeState>(builder: (context, state) {**
print(state);
if (state is JokeLoadingState) {
print("Loading state");
return const CircularProgressIndicator();
}
if (state is JokeLoadedState) {
print(state.jokeData.toString());
return ExpansionTile(
title: Text(state.jokeData.setup),
children: [
Container(
margin: const EdgeInsets.all(5.0),
child: Text(state.jokeData.delivery))
],
);
}
if (state is JokeErrorState) {
print("Error state");
return Text(state.error.toString());
}
return Container();
}),
ElevatedButton(
onPressed: () {
BlocProvider.of<JokeBloc>(context).add(LoadJokeEvent());
},
child: const Text('Load More'))
]),
);
}
}
下面是JokeBloc:
import 'package:bloc/bloc.dart';
import 'jokestate.dart';
import 'jokeevent.dart';
import 'package:api_call_sample/Repository/JokeRepository.dart';
class JokeBloc extends Bloc<JokeEvent,JokeState>{
final JokeRepository _jokeRepository;
JokeBloc(this._jokeRepository) : super(JokeLoadingState())
{
on<LoadJokeEvent>((event, emit) async {
emit(JokeLoadingState());
try {
final joke = await _jokeRepository.getJoke();
emit(JokeLoadedState(joke));
} catch (e) {
emit(JokeErrorState(e.toString()));
}
});
}
}
错误消息:
The following _CastError was thrown building _BodyBuilder:
type 'Null' is not a subtype of type 'JokeBloc' in type cast.
The relevant error-causing widget was:Scaffold Scaffold:file:///Applications/flutter/api_call_sample/lib/main.dart:37:12
The relevant error-causing widget was:
Scaffold Scaffold:file:///Applications/flutter/api_call_sample/lib/main.dart:37:12
When the exception was thrown, this was the stack:
#0 _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:786:72)
#1 _CreateInheritedProviderState.debugFillProperties (package:provider/src/inherited_provider.dart:806:44)
#2 _InheritedProviderScopeElement.debugFillProperties (package:provider/src/inherited_provider.dart:631:20)
#3 DiagnosticableNode.builder.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:2992:17)
#4 DiagnosticableNode.builder (package:flutter/src/foundation/diagnostics.dart:2995:8)
#5 DiagnosticableNode.getProperties (package:flutter/src/foundation/diagnostics.dart:3009:105)
#6 TextTreeRenderer._debugRender (package:flutter/src/foundation/diagnostics.dart:1244:63)
#7 TextTreeRenderer.render (package:flutter/src/foundation/diagnostics.dart:1121:14)
#8 DiagnosticsNode.toStringDeep.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:1776:9)
#9 DiagnosticsNode.toStringDeep (package:flutter/src/foundation/diagnostics.dart:1783:6)
#10 DiagnosticsNode.toString.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:1694:18)
#11 DiagnosticsNode.toString (package:flutter/src/foundation/diagnostics.dart:1706:6)
#12 Diagnosticable.toString.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:3168:78)
#13 Diagnosticable.toString (package:flutter/src/foundation/diagnostics.dart:3170:6)
#14 _StringBase._interpolate (dart:core-patch/string_patch.dart:853:19)
#15 RepositoryProvider.of (package:flutter_bloc/src/repository_provider.dart:78:12)
#16 MyApp.build.<anonymous closure> (package:api_call_sample/main.dart:20:36)
#17 _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:736:36)
#18 _InheritedProviderScopeElement.value (package:provider/src/inherited_provider.dart:590:33)
#19 Provider.of (package:provider/src/provider.dart:303:37)
#20 ReadContext.read (package:provider/src/provider.dart:649:21)
#21 _BlocBuilderBaseState.initState (package:flutter_bloc/src/bloc_builder.dart:130:36)
#22 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4942:57)
#23 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4781:5)
#24 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817:16)
#25 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6350:36)
#26 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6362:32)
... Normal element mounting (19 frames)
#45 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817:16)
#46 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6350:36)
#47 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6362:32)
... Normal element mounting (99 frames)
#146 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11)
... Normal element mounting (7 frames)
#153 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (7 frames)
#160 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11)
... Normal element mounting (275 frames)
#435 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817:16)
#436 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6350:36)
#437 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6362:32)
... Normal element mounting (377 frames)
#814 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817:16)
#815 Element.updateChild (package:flutter/src/widgets/framework.dart:3551:18)
#816 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1202:16)
#817 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1171:5)
#818 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1119:18)
#819 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2597:19)
#820 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1118:13)
#821 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:953:7)
#822 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:933:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
引发了另一个异常:每个子项必须恰好布局一次。
1条答案
按热度按时间noj0wjuj1#
在
BlocProvider
中创建JokeBloc
的方法在这个生命周期中不起作用,用下面的代码替换此代码应该可以解决此问题