所以我是 Flutter 的新手,但我试图弄清楚如何显示卡片的列表视图,每张卡片都有其详细信息。
我的问题是每次我尝试显示它时都会出现错误(见下文)。
根据我在网上找到的内容,他们说我需要使用扩展,因为我在小时候使用列内的小部件,我使用了它,但没有得到相同的错误。
任何人都可以帮助我,因为此时我不知道还能做什么,我现在被封锁了一天。
谢谢你。
下面我将添加代码和错误。
import 'dart:ui';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ipill/PharmacyNameClass.dart';
class SearchForMedicineScreen extends StatefulWidget {
@override
_SearchForMedicineScreenState createState() =>
_SearchForMedicineScreenState();
}
class _SearchForMedicineScreenState extends State<SearchForMedicineScreen> {
String NameOfThePharmacyFromSearchField = "";
final Pharmacy pharmacyName = Pharmacy("");
Future<QuerySnapshot> MedicineIGetAfterTheSearch;
List<Card> listOfPharmacies = [];
//Method that allows me seach for the drug
Future getYourDrugs(String NameOfThePharmacy) {
setState(() {
MedicineIGetAfterTheSearch = FirebaseFirestore.instance
.collection('Pharmacies')
.doc(NameOfThePharmacy)
.collection('Drugs')
.where('SearchKey',
isEqualTo: NameOfThePharmacyFromSearchField.substring(0, 1)
.toUpperCase())
.get();
});
return MedicineIGetAfterTheSearch;
}
Widget projectWidget() {
return FutureBuilder<QuerySnapshot>(
future: MedicineIGetAfterTheSearch,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.connectionState == ConnectionState.done) {
QuerySnapshot dataIWorkWithAfterIGetTheMedicines = snapshot.data;
for (int i = 0;
i < dataIWorkWithAfterIGetTheMedicines.docs.length;
i++) {
Card temContainer = Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.local_hospital),
title: Text(
"Name ${dataIWorkWithAfterIGetTheMedicines.docs[i].get("Name")}"),
subtitle: Text(
"Price ${dataIWorkWithAfterIGetTheMedicines.docs[i].get("Price")}"),
trailing: Text(
"Quantity ${dataIWorkWithAfterIGetTheMedicines.docs[i].get("Quantity")}"),
),
Row(
children: <Widget>[
Column(
children: <Widget>[
IconButton(
onPressed: () {},
icon: Image.asset("shopicon.png"),
)
],
),
],
),
],
),
);
if (temContainer == null) {
listOfPharmacies = [];
}
listOfPharmacies.add(temContainer);
}
return ListView.builder(
itemCount: listOfPharmacies.length,
itemBuilder: (BuildContext context, int index) {
return listOfPharmacies[index];
});
}
return Text("loading");
});
}
@override
Widget build(BuildContext context) {
Pharmacy pharmacyName = ModalRoute.of(context).settings.arguments;
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 20,
),
Row(
children: <Widget>[
TextField(
onChanged: (val) {
setState(() {
if (val == null) {
listOfPharmacies = [];
}
NameOfThePharmacyFromSearchField = val;
listOfPharmacies = [];
getYourDrugs(pharmacyName.name);
});
},
),
],
),
SizedBox(
height: 20,
),
projectWidget(),
],
),
),
);
}
}
这是错误:
======== Exception caught by rendering library =====================================================
RenderBox was not laid out: RenderFlex#e9630 relayoutBoundary=up2 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1785 pos 12: 'hasSize'
The relevant error-causing widget was:
SafeArea file:///D:/2_Flutter/ipill/lib/SearchForMedicineScreen.dart:99:13
====================================================================================================
======== Exception caught by rendering library =====================================================
RenderBox was not laid out: RenderPadding#455b0 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1785 pos 12: 'hasSize'
The relevant error-causing widget was:
Scaffold file:///D:/2_Flutter/ipill/lib/SearchForMedicineScreen.dart:98:12
====================================================================================================
Reloaded 4 of 798 libraries in 1,312ms.
======== Exception caught by rendering library =====================================================
The following _CastError was thrown during paint():
Null check operator used on a null value
The relevant error-causing widget was:
Column file:///D:/2_Flutter/ipill/lib/SearchForMedicineScreen.dart:100:16
When the exception was thrown, this was the stack:
#0 RenderFlex._hasOverflow (package:flutter/src/rendering/flex.dart:487:37)
#1 RenderFlex.paint (package:flutter/src/rendering/flex.dart:970:10)
#2 RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2311:7)
#3 PaintingContext.paintChild (package:flutter/src/rendering/object.dart:189:13)
#4 RenderShiftedBox.paint (package:flutter/src/rendering/shifted_box.dart:70:15)