我正在使用 flutter bloc 来显示下载进度百分比,但我不断遇到此问题。我认为问题出现在onDone方法中,但我不知道如何解决它。
错误 :
发生异常。_AssertionError ('package:bloc/src/bloc.dart': 断言失败: 第 137 行 pos 7: '!_isCompleted': 在事件处理程序正常完成后调用了emit。这通常是由于事件处理程序中未等待的 future 造成的。请确保使用事件处理程序等待所有异步操作,并在异步操作后调用emit() 之前使用emit.isDone 以确保事件处理程序尚未完成。
坏的
on<Event>((event, emit) {
future.whenComplete(() => emit(...));
});
Run Code Online (Sandbox Code Playgroud)
好的
on<Event>((event, emit) async {
await future.whenComplete(() => emit(...));
});
)
Run Code Online (Sandbox Code Playgroud)
代码 :
import 'package:bloc/bloc.dart';
import 'package:download_progress_with_bloc/downlaod_file.dart';
import 'package:download_progress_with_bloc/download_event.dart';
import 'package:download_progress_with_bloc/download_state.dart';
import 'package:download_progress_with_bloc/permission_handler.dart';
import 'package:download_progress_with_bloc/store_book_repo.dart';
import 'package:http/http.dart' as http;
class DownloadBloc extends Bloc<DownloadEvent, DownloadState> {
DownloadBloc({required this.storeBookRepo}) : super(DownloadInitial()) {
on<DownloadStarted>(onStarted);
on<DownloadProgressed>(onProgressed);
}
final StoreBookRepo storeBookRepo;
http.StreamedResponse? response;
// StreamController? …Run Code Online (Sandbox Code Playgroud) 我RadioListTile在我的应用程序中使用该小部件,并且我想在满足某些条件后禁用更改该值。并且没有isButtonDisabled像Radiowidget 中那样的 prop。我怎样才能禁用这个小部件(阻止值改变)?
当我写下上面的陈述时,我已经弄清楚了,这很容易。所以我会发布这个问题并在下面回答。
import 'package:flutter/material.dart';
class Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<Body> {
bool evaluate = false;
int value;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
RadioListTile<int>(
value: 0,
isThreeLine: false,
title: Text(
'radio 1',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
groupValue: value,
onChanged: (value) => evaluate ? null : value = 0,
activeColor: Colors.green,
),
RadioListTile<int>(
value: 1, …Run Code Online (Sandbox Code Playgroud) 嘿,我一直将块观察者作为我的 flutter 应用程序中的主要状态管理工具,并且使用它使事情变得更加容易。块观察者是我用来调试和观察发生的事情的主要工具。但迁移到 Bloc v8.0.0 后,bloc 观察者已停止记录。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
HttpOverrides.global = MyHttpOverrides();
await Hive.initFlutter();
Hive.registerAdapter(UserAdapter());
await Hive.openBox<User>('user');
await Firebase.initializeApp();
BlocOverrides.runZoned(
() {},
blocObserver: SimpleBlocObserver(),
);
...
}
Run Code Online (Sandbox Code Playgroud)
这是主要功能的片段
Bloc observer
import 'package:flutter_bloc/flutter_bloc.dart';
class SimpleBlocObserver extends BlocObserver {
@override
void onEvent(Bloc bloc, Object? event) {
super.onEvent(bloc, event);
print(event);
}
@override
void onChange(BlocBase bloc, Change change) {
super.onChange(bloc, change);
print(change);
}
@override
void onCreate(BlocBase bloc) {
super.onCreate(bloc);
print(bloc);
}
@override
void onTransition(Bloc bloc, Transition transition) {
super.onTransition(bloc, transition);
print(transition); …Run Code Online (Sandbox Code Playgroud) 我一直在尝试将 Flutter 定制SearchDelgate为我想要的搜索字段类型。appBarTheme它有一个名为返回类型的方法ThemeData。通常使用ThemeData您可以更改应用栏主题,但在我的情况下它没有进行任何更改。我可以自定义提示文本样式searchFieldStyle方法,但仅此而已。
这是代码:
class CustomSearchDelegate extends SearchDelegate<Country> {
@override
ThemeData appBarTheme(BuildContext context) {
return ThemeData(
appBarTheme: AppBarTheme(
elevation: 0,
color: themeColor,
//app bar color I wanted
),
);
}
@override
TextStyle get searchFieldStyle => TextStyle(
color: whiteTextColor,
fontWeight: FontWeight.w600,
fontFamily: GoogleFonts.poppins().fontFamily,
);
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(
Icons.close_rounded,
color: Colors.white,
),
onPressed: () => query = '',
),
];
}
@override
Widget buildLeading(BuildContext context) …Run Code Online (Sandbox Code Playgroud)