当我制作复选框数据并将选定的复选框显示到下一个屏幕时,我遇到了上述错误。我只是在做复制粘贴,我对代码一窍不通。
import 'package:flutter/material.dart';
void main() => runApp(Test());
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Select Services',
home: RandomWords(),
);
return Container();
}
}
这是一个无状态小部件
class _RandomWordsState extends State<RandomWords>{
final _suggestions = <String>['this is me1','this is me2','this is me3' ];
final _saved = <String>['this is me1','this is me2','this is me3' ];
final _biggerFont = TextStyle(fontSize: 18.0);
这里我定义了列表视图。
void _pushSaved(){
Navigator.of(context).push(
MaterialPageRoute<void>(
// NEW lines from here...
builder: (BuildContext context) {
final tiles = _saved.map(
(String pair) {
return ListTile(
title: Text(
pair,
style: _biggerFont,
),
);
},
);
final divided = ListTile.divideTiles(
context: context,
tiles: tiles,
).toList();
return Scaffold(
appBar: AppBar(
title: Text('Saved Suggestions'),
),
body: ListView(children: divided),
);
}, // ...to here.
),
);
}
Widget _buildSuggestions() {
return ListView.builder(
padding: EdgeInsets.all(16.0),
itemBuilder: /*1*/ (context, i) {
if (i.isOdd) return Divider(); /*2*/
我在本节中遇到错误,错误为 "error: The method 'generateWordPairs' is not defined for the type '_RandomWordsState'. (undefined_method at [ABC] lib\pages\test.dart: 327)”在控制台中。
final index = i ~/ 2; /*3*/
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10));
}
return _buildRow(_suggestions[index]);
});
}
这是 Rest 代码。
Widget _buildRow(String pair) {
final alreadySaved = _saved.contains(pair);
return ListTile(
title: Text(
pair,
style: _biggerFont,
),
trailing: Icon(
alreadySaved ? Icons.check_box : Icons.check_box_outlined,
color: alreadySaved ? Colors.cyan :null,
),
onTap: (){
setState(() {
if (alreadySaved){
_saved.remove(pair);
}
else{
_saved.add(pair);
}
});
},
);
}
这是一个新的屏幕代码
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Startup Name Generator'),
actions: [
IconButton(icon: Icon(Icons.chevron_right), onPressed: _pushSaved),
],
),
body: _buildSuggestions(),
);
}
}
答案 0 :(得分:0)
删除这些行:
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10));
}
然后将其添加到您的 _buildSuggestions() 函数之上,如果您以后想更改它
Widget _buildSuggestions() {
_suggestions = <String>['this is me1','this is me2','this is me3'];
return ListView.builder( ...
如果没有,只需删除这些行,不要添加任何其他内容。
您需要导入包 english_words 才能使用 generateWordPairs() 函数。
(至于 26.04.2021)这是最新版本
english_words: ^4.0.0
像这样导入:
import 'package:english_words/english_words.dart';