gpt4 book ai didi

dart - 尝试调用 : ancestorStateOfType(Instance of 'TypeMatcher'

转载 作者:IT王子 更新时间:2023-10-29 07:20:58 25 4
gpt4 key购买 nike

我正在尝试在 ListView 中点击行项目来调用新页面,我对 flutter 很陌生

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: "My First Flutter App", home: new Home());
}
}

class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: AppBar(
title: Text("Hello"),
backgroundColor: Colors.black,
),
body: WordPairState()._buildSugg(context));
}
}

class WordPairState extends State<RandomWordPair> {
final _suggetions = <WordPair>[];
final _bigText = const TextStyle(fontSize: 16);

@override
Widget build(BuildContext context) {
final _wordPair = WordPair.random();
// TODO: implement build
return Text(_wordPair.asPascalCase);
}

Widget _buildSugg(BuildContext ctx) {
return ListView.builder(
padding: const EdgeInsets.all(10),
itemBuilder: (context, i) {
if (i.isOdd) return Divider();

final count = i ~/ 2;

if (count >= _suggetions.length) {
_suggetions.addAll(generateWordPairs().take(10));
}

return _buildRow(_suggetions[count], count, ctx);
});
}

Widget _buildRow(WordPair wp, final count, BuildContext ctx) {
return ListTile(
subtitle: Text("List Sub title " + wp.toString()),
title: Text(
wp.asPascalCase,
style: _bigText,
),
onTap: () {
Route route = MaterialPageRoute(builder: (context) => SecondRoute());

Navigator.push(context, route);
});
}

我收到如下错误,可能是因为上下文错误,但我不知道应该传递哪个上下文:

══╡ 手势异常捕获════════════════════════════I/flutter (15113): 处理手势时抛出以下 NoSuchMethodError:I/flutter (15113):在 null 上调用了方法“ancestorStateOfType”。I/flutter (15113): 接收器: nullI/flutter(15113):尝试调用:ancestorStateOfType('TypeMatcher'的实例)我/flutter (15113):I/flutter (15113): 抛出异常时,栈是这样的:我/flutter (15113): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)I/flutter (15113): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:1376:19)I/flutter (15113):#2 WordPairState._buildRow。 (包:flutter_app/main.dart:99:19)I/flutter (15113): #3 _InkResponseState._handleTap (包:flutter/src/material/ink_well.dart:513:14)I/flutter (15113):#4 _InkResponseState.build。 (包:flutter/src/material/ink_well.dart:568:30)I/flutter (15113): #5 GestureRecognizer.invokeCallback (包:flutter/src/gestures/recognizer.dart:120:24)I/flutter (15113): #6 TapGestureRecognizer._checkUp (包:flutter/src/gestures/tap.dart:242:9)I/flutter (15113): #7 TapGestureRecognizer.acceptGesture (包:flutter/src/gestures/tap.dart:204:7)I/flutter (15113): #8 GestureArenaManager.sweep (包:flutter/src/gestures/arena.dart:156:27)I/flutter (15113): #9 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (包:flutter/src/gestures/binding.dart:218:20)I/flutter (15113): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (包:flutter/src/gestures/binding.dart:192:22)I/flutter (15113): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (包:flutter/src/gestures/binding.dart:149:7)I/flutter (15113): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (包:flutter/src/gestures/binding.dart:101:7)I/flutter (15113): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (包:flutter/src/gestures/binding.dart:85:7)我/flutter (15113): #17 _invoke1 (dart:ui/hooks.dart:223:10)我/flutter (15113): #18 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)I/flutter (15113):(从包 dart:async 中删除了 3 帧)我/flutter (15113):I/flutter (15113): 处理程序:onTap

最佳答案

你的方法有错误/错别字

 Widget _buildRow(WordPair wp, final count, BuildContext context) { // you named it ctx (and using context in implementation) which was causing the problem
return ListTile(
subtitle: Text("List Sub title " + wp.toString()),
title: Text(
wp.asPascalCase,
style: _bigText,
),
onTap: () {
Route route = MaterialPageRoute(builder: (context) => SecondRoute());

Navigator.push(context, route);
});
}

关于dart - 尝试调用 : ancestorStateOfType(Instance of 'TypeMatcher<NavigatorState>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55656851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com