直到今天,将数据保存到firebase都没有问题。我认为这是相同的代码,我使用大部分时间来存储在firebase中的数据,但奇怪的是收集"LoanFriends"没有被送到消防基地。我可能遗漏了一些细节。下面的代码不会在android studio中产生任何错误,但是数据没有被保存。
代码如下:
import 'package:budgetapp/ExtractedWidgets/dialog_field.dart';
import 'package:budgetapp/constant.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class WalletScreen extends StatelessWidget {
static String id = 'wallet_screen';
TextEditingController _controllername = TextEditingController();
TextEditingController _controlleraddress =TextEditingController();
TextEditingController _controllerphoneno = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('My Wallet',style: kheadertext),
elevation: 0,
actions: [IconButton(onPressed: (){}, icon: Icon(Icons.notifications_sharp),),],
),
body: Column(
children: [
Container(
height: 200,
decoration: containerdecoration.copyWith( borderRadius: BorderRadius.circular(32.0),),
margin: EdgeInsets.all(15),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Container(
padding: EdgeInsets.only(left: 35),
child: Row(
children: [
Text('Balance',style: kmyTextstyle.copyWith(fontSize: 25),),
SizedBox(width: 200,),
Icon(FontAwesomeIcons.moneyBill,
color: Colors.white,),
],
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.only(left: 35),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text('Rs. ', style: kmyTextstyle.copyWith(fontSize: 35),),
SizedBox(width: 5,),
Text("25000",style: kmyTextstyle.copyWith(fontSize: 35),),
],),
),
),
Expanded(
child: Container(
padding: EdgeInsets.only(left: 35),
child: Row(
children: [
Text('My wallet information',style: kmyTextstyle.copyWith(fontSize: 15),),
SizedBox(width: 150,),
Icon(FontAwesomeIcons.ccMastercard,
color: Colors.white,),
],
),
),
),
],
)
),
Text("Loan Mechanism",style: kheadertext.copyWith(fontSize: 20),),
SizedBox(height: 8,),
StreamBuilder<QuerySnapshot?>(
stream: FirebaseFirestore.instance.collection('LoanFriends').snapshots(),
builder: (context,snapshot){
if(!snapshot.hasData){
return CircularProgressIndicator();
}
else {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.docs.length,
itemBuilder:(context, index){
return Text(snapshot.data!.docs[index]['name']);
});
}
}
),
],
),
floatingActionButton: FloatingActionButton(
backgroundColor: Color(0xFFcdc3ff),
onPressed: (){
showAlertDialog(context);
},
child: Icon(Icons.add),
),
);
}
void showAlertDialog(BuildContext context) {
showDialog(context: context, builder: (context){
return AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(32), ),
content: Container(
height: MediaQuery.of(context).size.height*0.4,
width: MediaQuery.of(context).size.width*0.5,
child: Column(
children: [
Dialogfield(hinttext: 'Enter name', controller: _controllername, textInputtype: TextInputType.name),
Dialogfield(hinttext: 'Enter address', controller: _controlleraddress, textInputtype: TextInputType.text),
Dialogfield(hinttext: 'Enter phonenumber', controller: _controllerphoneno, textInputtype: TextInputType.number),
TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
Color(0xFFcdc3ff)),
),
onPressed: (){
Map<String, dynamic> mapdata = {'name': _controllername, 'address': _controlleraddress, 'phonenumber': _controllerphoneno
};
FirebaseFirestore.instance.collection('LoanFriends').add(mapdata);
_controllername.text ='';
_controlleraddress.text='';
_controllerphoneno.text ='';
Navigator.pop(context);
},
child: Text('Add User',style: kmyTextstyle.copyWith(fontSize: 15),),),
],
),
),
);
});
}
}
需要访问controller
的text
属性。不能直接引用controller
改变这一行:
Map<String, dynamic> mapdata = {'name': _controllername, 'address': _controlleraddress, 'phonenumber': _controllerphoneno
插入这行:
Map<String, dynamic> mapdata = {'name': _controllername.text, 'address': _controlleraddress.text, 'phonenumber': _controllerphoneno.text