Flutter又出现了一个以前类似的问题,以前的问题莫名其妙的就好了,现在又一个错误了,请高手帮忙看一下,问题在哪里?我把ID修改为string还是同样的错误,应该不是数据格式的问题:
import 'package:flutter/material.dart';
import 'package:searchable_listview/searchable_listview.dart';
import 'Zbencaogangmu.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SafeArea(
child: ExampleApp(),
),
),
);
}
}
class ExampleApp extends StatefulWidget {
ExampleApp({Key? key}) : super(key: key);
@override
_ExampleAppState createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
late int id;
late String name;
late String content;
final List<Actor> actors = [];
@override
void initState() {
super.initState();
// if (mounted) {
// setState(() {
for (int i = 1; i < listBCGMData.length; i++) {
actors.add(listBCGMData[i]['ID']);
actors.add(listBCGMData[i]['Name']);
actors.add(listBCGMData[i]['Content']);
// actors.add(listBCGMData[i]['ID']);
// actors.add(listBCGMData[i]['Name']);
// actors.add(listBCGMData[i]['Content']);
print('Actors:');
print(actors);
}
// });
// }
}
@override
Widget build(BuildContext context) {
print('Actors:');
print(actors);
return SizedBox(
width: double.infinity,
child: Column(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(15),
child: SearchableList<Actor>(
initialList: actors,
builder: (dynamic user) => UserItem(user: user),
filter: _filterUserList,
emptyWidget: const EmptyView(),
inputDecoration: InputDecoration(
labelText: "Search Actor",
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.blue,
width: 1.0,
),
borderRadius: BorderRadius.circular(10.0),
),
),
),
),
)
],
),
);
}
List<Actor> _filterUserList(search) {
return actors
.where(
(element) => element.name.contains(search),
// (element) => element.Name.toLowerCase().contains(search),
)
.toList();
}
}
class UserItem extends StatelessWidget {
final Actor user;
const UserItem({
Key? key,
required this.user,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 60,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: [
const SizedBox(
width: 10,
),
Icon(
Icons.star,
color: Colors.yellow[700],
),
const SizedBox(
width: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'FirstName: ${user.name}',
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
Text(
'Content: ${user.content}',
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
Text(
'ID: ${user.id}',
style: const TextStyle(
color: Colors.black,
),
),
],
),
],
),
),
);
}
}
class EmptyView extends StatelessWidget {
const EmptyView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.error,
color: Colors.red,
),
Text('no actor is found with this Name'),
],
);
}
}
class Actor {
int id;
String name;
String content;
Actor({
required this.id,
required this.name,
required this.content,
});
}
//=================================================
listBCGMData 的数据格式:
List listBCGMData = [
{
"ID": 21661,
"Name": "榲柏",
"Content": "榲柏 \n释名:酸、甘、微温、无毒气味和性质: \n主治:\n主治温中,下气,消食,止渴,散酒气。 ",
"Key": "榲柏 \n释名:酸、甘、微温、无毒气味和性质: \n主治:\n主治温中,下气,消食,止渴,散酒气。 "
},
{
"ID": 21662,
"Name": "阿胶",
"Content":
"阿胶 \n释名:傅致胶。\n气味和性质:\n甘、平、无毒 \n主治:\n1、瘫缓偏风,手足不遂,腰膝无力。用阿胶微炙熟,先以水一升,煮香豉二合,去渣,以汁和入胶中,再煮开几次,胶化如糖稀,一次服下。服后取葱豉粥温服(不能冷服,否则令人呕逆)。照此法服至三、四剂,可见效。 \n2、肺风喘促。用秀明阿胶切小,炒过,加紫苏、乌梅肉(焙、研)等分,水煎服。 \n3、老人虚秘。用阿胶(炒)二钱、葱白三根,水煎化,加蜜暗无天日匙,温服。 \n4、赤白简疾(肠胃气虚,冷热不调,不痢赤白,里急后重,腹痛,不便不利)。用阿胶(炒过,水化成膏)一两、黄连三两、茯苓二两,共捣匀做成丸子,如梧子大。每服五十丸,粟米汤送下。一天服三次,此方名“黄连阿胶丸”。 \n5、吐血不止。用阿胶(炒)二两、蒲黄六合、生地黄三升,加水五程式,煮成三升,分次服。又方:用阿胶(炒)、蛤粉各一两,辰砂少许,研为末,藕节捣汁,加蜜调匀服下。 \n6、肺损呕血。用阿胶(炒)三钱、水香一钱,糯米一合半(研为末),和匀。每服一钱,百沸汤冲下。一天服一次。 \n7、鼻血不止(口耳都流血)。用阿胶炙蒲黄半两,每取二钱,加水一碗,生地黄汁一合,煎至六成。温服。同时以布系住两乳。 \n8、月经不调。用阿胶一钱,加蛤粉(炒成珠,研为末),热酒送服。又方:依上方,再加辰砂末半钱。 \n9、月经不断。用阿胶炒焦研为末,酒送服二钱。 \n10、妊娠下血。用阿胶三两,炙为末,酒一升半煎化服下。又方:用阿胶末二两、生地黄半斤(捣成汁)、加水四升,煮成一升,分次服。此方名“胶艾汤”。 \n11、妊娠胎动。用香豉一升、葱一升、加水三升,煮取一升,再加进阿胶(炙过,研细)二两,化匀服下。又方:用阿胶(炒熟)、艾叶二两、葱白一斤,加水四升,煮成一升,分次服。此方名“胶艾汤” 12、多年咳嗽。用阿胶(炒)、人参各二两,研细。每取三钱,加豉汤一碗、葱白少许,煎服。一天服三次。 ",
"Key":
"阿胶 \n释名:傅致胶。\n气味和性质:\n甘、平、无毒 \n主治:\n1、瘫缓偏风,手足不遂,腰膝无力。用阿胶微炙熟,先以水一升,煮香豉二合,去渣,以汁和入胶中,再煮开几次,胶化如糖稀,一次服下。服后取葱豉粥温服(不能冷服,否则令人呕逆)。照此法服至三、四剂,可见效。 \n2、肺风喘促。用秀明阿胶切小,炒过,加紫苏、乌梅肉(焙、研)等分,水煎服。 \n3、老人虚秘。用阿胶(炒)二钱、葱白三根,水煎化,加蜜暗无天日匙,温服。 \n4、赤白简疾(肠胃气虚,冷热不调,不痢赤白,里急后重,腹痛,不便不利)。用阿胶(炒过,水化成膏)一两、黄连三"
},
{
"ID": 21663,
"Name": "阿井泉",
"Content":
"阿井泉 \n释名:阿井泉也是一种井水。\n气味和性质:\n气味甘咸、平、无毒。 \n主治:\n 逐痰下降(即治逆痰),下胸胃淤浊,止吐。\n附方:\n阿井在今山东阳谷县,即古代东阿县。井下水直通济南,所以济南的井水,也有阿井水的作用。这支水叫作济水。早在周代,《管子》就记载着",
"Key":
"阿井泉 \n释名:阿井泉也是一种井水。\n气味和性质:\n气味甘咸、平、无毒。 \n主治:\n 逐痰下降(即治逆痰),下胸胃淤浊,止吐。\n附方:\n阿井在今山东阳谷县,即古代东阿县。井下水直通济南,所以济南的井水,也有阿井水的作用。这支水叫作济水。早在周代,《管子》就记载着 "
},
//=========================
谢谢