Flutter使用syncfusion_flutter_sliders触发AssertionError_throwNew错误求助
问题排查:使用syncfusion_flutter_sliders触发AssertionError_throwNew错误
问题描述
我是Flutter新手,尝试用第三方库syncfusion_flutter_sliders实现带刻度的水平滑块,编写了对应的有状态Widget代码。但在另一个无状态路由中调用该Widget时,触发了errors_patch.dart文件中的AssertionError_throwNew错误,已参考官方库文档,请求帮助排查问题。
实现代码
class SliderExample extends StatefulWidget { const SliderExample({super.key}); @override State<SliderExample> createState() => _SliderExampleState(); } class _SliderExampleState extends State<SliderExample> { final double _min = 0; final double _max = 100; double _value = 40.0; @override Widget build(BuildContext context) { return Scaffold( body: Center( child: SfSlider( min: _min, max: _max, value: _value, interval: 20, showTicks: true, showLabels: true, onChanged: (dynamic newValue) { setState(() { _value = newValue; }); }, ), ), ); } }
错误信息
@pragma("vm:external-name", "AssertionError_throwNew") external static _doThrowNew( int assertionStart, int assertionEnd, Object? message);
调用代码
return Scaffold( body: Container( alignment: Alignment.center, decoration: const BoxDecoration( gradient: LinearGradient(colors: [Colors.orangeAccent, Colors.deepOrange]), ), child: ListView( children: const <Widget>[ Text('\n\n\nHow Many People Did You Meet Today?', textAlign: TextAlign.justify, style: TextStyle(fontSize: 40, color: Colors.black, fontWeight: FontWeight.bold) ), MaterialApp( home: SliderExample(), // <-- 问题所在 ) ] ), ), );
解决方案
核心问题
调用代码中错误地将MaterialApp作为ListView的子Widget嵌套。MaterialApp是Flutter应用的根Widget,负责管理全局主题、导航器等核心服务,不能嵌套在其他Widget(如ListView)内部,这种结构会导致上下文冲突,触发断言错误。
修改后的调用代码
直接移除嵌套的MaterialApp,将SliderExample作为ListView的子Widget即可:
return Scaffold( body: Container( alignment: Alignment.center, decoration: const BoxDecoration( gradient: LinearGradient(colors: [Colors.orangeAccent, Colors.deepOrange]), ), child: ListView( children: const <Widget>[ Text('\n\n\nHow Many People Did You Meet Today?', textAlign: TextAlign.justify, style: TextStyle(fontSize: 40, color: Colors.black, fontWeight: FontWeight.bold) ), // 直接使用SliderExample,无需嵌套MaterialApp SliderExample() ] ), ), );
额外检查
确保已在pubspec.yaml中正确添加syncfusion_flutter_sliders依赖:
dependencies: flutter: sdk: flutter syncfusion_flutter_sliders: ^25.1.35 # 使用最新版本
内容的提问来源于stack exchange,提问作者Harshal Janjani
相关产品推荐
相关产品推荐

