我尝试在块的 Getx Rx 上添加错误catch
并在我的流生成器上获取它,但是得到以下错误并且进度指示器一直运行:
[GETX] GOING TO ROUTE /
[GETX] REMOVING ROUTE /login
E/flutter ( 5129): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: Closure call with mismatched arguments: function '_StreamBuilderBaseState._subscribe.<anonymous closure>'
E/flutter ( 5129): Receiver: Closure: (Object, StackTrace) => Null
E/flutter ( 5129): Tried calling: _StreamBuilderBaseState._subscribe.<anonymous closure>("error")
E/flutter ( 5129): Found: _StreamBuilderBaseState._subscribe.<anonymous closure>(Object, StackTrace) => Null
E/flutter ( 5129): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter ( 5129): #1 GetStream._notifyError [package:get/…/rx_stream/get_stream.dart]
E/flutter ( 5129): #2 GetStream.addError [package:get/…/rx_stream/get_stream.dart]
E/flutter ( 5129): #3 _RxImpl.addError [package:get/…/rx_core/rx_impl.dart]
E/flutter ( 5129): #4 HomeController.load [package:testegetx/main.dart]
E/flutter ( 5129): <asynchronous suspension>
E/flutter ( 5129):
这是我用来重现错误的代码:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'App',
initialRoute: '/login',
getPages: [
GetPage(name: '/login', page: () => LoginPage()),
GetPage(name: '/', page: () => HomePage(HomeController())),
],
);
}
}
class LoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Get.offAllNamed('/');
},
child: Text('Go Home'),
),
),
);
}
}
class HomeController {
final _text = Rx<String>();
Stream<String> get textStream => _text.stream;
Future<void> load() async {
try {
await Future.delayed(Duration(seconds: 2));
throw Error();
} catch (e) {
_text.addError('error');
}
}
}
class HomePage extends StatelessWidget {
final HomeController controller;
HomePage(this.controller);
@override
Widget build(BuildContext context) {
controller.load();
return Scaffold(
body: StreamBuilder<String>(
stream: controller.textStream,
builder: (context, snapshot) {
Widget child;
if (snapshot.hasError) {
child = Text(snapshot.error);
} else if (!snapshot.hasData) {
child = Text('loading...');
} else {
child = Text(snapshot.data);
}
return Center(
child: child,
);
}),
);
}
}
更新
我搬到了稳定的频道,它在评论中得到了回答。