在Flutter中使用StreamProvider时出现断言错误

时间:2019-05-26 05:54:27

标签: flutter

我想在我的flutter应用程序中使用提供程序包。为了访问用户位置,我创建了 LocationProvider 类,该类内部具有位置数据流。我希望我的子小部件访问此位置数据,因此我使用了 StreamProvider.value()方法。但是,它不起作用,并返回断言失败:'builder!= null':不正确。我不知道为什么它会一直失败,因为 StreamProvider()而不是 StreamProvider.value()

class LocationProvider extends StreamProvider {
  final Location location = new Location();

  Stream<LocationData> get currentLocation =>
      location.getLocation().asStream();

}
 @override
  Widget build(BuildContext context) {
    return StreamProvider.value(
        child: new MaterialApp(
        title: 'Sharify',
        theme: new ThemeData(
          primaryColor: Colors.white,
        ),
        home: new ToolMap(),
      ), stream: LocationProvider().currentLocation,
    );
  }

Stacktrace:

I/flutter (29561): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (29561): The following assertion was thrown building MyApp(dirty, state: AppState#95d57):
I/flutter (29561): 'package:provider/src/adaptive_builder_widget.dart': Failed assertion: line 10 pos 16: 'builder !=
I/flutter (29561): null': is not true.
I/flutter (29561):
I/flutter (29561): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (29561): more information in this error message to help you determine and fix the underlying cause.
I/flutter (29561): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (29561):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (29561):
I/flutter (29561): When the exception was thrown, this was the stack:
I/flutter (29561): #2      new AdaptiveBuilderWidget 
package:provider/src/adaptive_builder_widget.dart:10
I/flutter (29561): #3      new StreamProvider 
package:provider/src/stream_provider.dart:40
I/flutter (29561): #4      new LocationProvider (package:sharify/tool_list/providers/location_provider.dart)
I/flutter (29561): #5      AppState.build 
package:sharify/main.dart:48
I/flutter (29561): #6      StatefulElement.build 
package:flutter/…/widgets/framework.dart:3825
I/flutter (29561): #7      ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:3739
I/flutter (29561): #8      Element.rebuild 
package:flutter/…/widgets/framework.dart:3565
I/flutter (29561): #9      BuildOwner.buildScope 
package:flutter/…/widgets/framework.dart:2278
I/flutter (29561): #10     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame 
package:flutter/…/widgets/binding.dart:700
I/flutter (29561): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback 
package:flutter/…/rendering/binding.dart:286
I/flutter (29561): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback 
package:flutter/…/scheduler/binding.dart:1012
I/flutter (29561): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame 
package:flutter/…/scheduler/binding.dart:952
I/flutter (29561): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> 
package:flutter/…/scheduler/binding.dart:773
I/flutter (29561): #16     _Timer._runTimers  (dart:isolate-patch/timer_impl.dart:382:19)
I/flutter (29561): #17     _Timer._handleMessage  (dart:isolate-patch/timer_impl.dart:416:5)
I/flutter (29561): #18     _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:171:12)

0 个答案:

没有答案