私はと呼ばれる方法で持っている_sendToDatabaseWithArgs
いくつかの引数を取り、firestoreデータベースに保存ONTAP。引数は、フォームがあり、3つの入力を受け取る最初の画面からすでに入力されています。
問題は、onTapアクションを実行すると、データがfirestoreに保存されないことです。しかし、プログラムを保存すると、データは自動的にデータベースに保存されます!!!!
これを処理することになっているクラスは次のとおりです。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'StoreData.dart';
class FeedBack extends StatelessWidget {
final StoreData storeData;
String feedBackHolder = "";
FeedBack({Key key, @required this.storeData}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Navajo Inc Feedback"),
),
body: new Container(
padding: new EdgeInsets.all(15.0),
child: new ListView(
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new RaisedButton(onPressed: _sendToDatabaseWithArgs(storeData.storeNumber, storeData.repName, storeData.repCell, storeData.transactionDate),
child: const Text('RAISED BUTTON'),
),
new GestureDetector(
onTap: _sendToDatabaseWithArgs(storeData.storeNumber, storeData.repName, storeData.repCell, storeData.transactionDate),
child: feedBackCardsImage(
imagePath: 'assets/happy_face.png',
CardTitle: 'Happy Customer',
CardTag: 'Experience was excellent',
),
),
new GestureDetector(
onTap: _sendToDatabase,
child: feedBackCardsImage(
imagePath: 'assets/neutral_face.png',
CardTitle: 'Neutral Customer',
CardTag: 'Experience was satisfactory',
),
),
new GestureDetector(
onTap: _sendToDatabase,
child: feedBackCardsImage(
imagePath: 'assets/angry_face.png',
CardTitle: 'Unsatisfied Customer',
CardTag: 'Experience was unsatisfactory',
),
),
],
),
),
);
}
_sendToDatabaseWithArgs(String storeNumber,repName,repCell, DateTime transactionDate) {
Firestore.instance.runTransaction((Transaction transaction) async {
CollectionReference reference = Firestore.instance.collection('Stores');
await reference.add({
"store Name": storeNumber,
"Rep Name": repName,
"Rep Cell": repCell,
"Transaction Date": transactionDate,
});
});
}
_sendToDatabase() {
Firestore.instance.runTransaction((Transaction transaction) async {
CollectionReference reference = Firestore.instance.collection('Stores');
await reference.add({
"store Name": "${storeData.storeNumber}",
"Rep Name": "${storeData.repName}",
"Rep Cell": "${storeData.repCell}",
"Transaction Date":"${storeData.transactionDate}",
});
});
}
}
// feedback cardsImage widget / class
class feedBackCardsImage extends StatelessWidget {
final String imagePath;
final String CardTitle;
final String CardTag;
feedBackCardsImage({
this.imagePath,
this.CardTitle,
this.CardTag,
});
@override
Widget build(BuildContext context) {
return new Container(
child: new Card(
child: new Padding(
padding: new EdgeInsets.all(15.0),
child: new Column(
children: <Widget>[
new SizedBox(
height: 184.0,
child: new Stack(
children: <Widget>[
new Positioned.fill(
child: new Image.asset(
imagePath,
//package: destination.assetPackage,
fit: BoxFit.contain,
),
),
],
),
),
new Padding(padding: new EdgeInsets.all(7.0,),
child: new Text(CardTitle, style: new TextStyle(fontSize: 14.0, fontWeight: FontWeight.w600, color: Colors.black87), ),
),
new Padding(padding: new EdgeInsets.all(0.0,),
child: new Text(CardTag, style: new TextStyle(fontSize: 12.0, fontWeight: FontWeight.w400, color: Colors.black54), ),
),
],
),
),
),
);
}
}
どんな洞察もいただければ幸いです!
あなたの問題は、Flutterが_sendToDatabaseWithArgs
onTapが呼び出されたときにメソッドを呼び出すのではなく、onTapを呼び出した結果を割り当てていることだと思います。そのため_sendToDatabaseWithArgs
、アプリが保存/ホットリロードされたときに実行されます。ウィジェットをロードするときにメソッドを呼び出しています。
これを修正するには、onTap
sを次のように変更します。
onTap: () => _sendToDatabaseWithArgs(...)
このように、Flutterは、メソッドを呼び出してウィジェットがタップされたときに呼び出される呼び出し可能なオブジェクトを返すことを期待するのではなく、正しい引数を使用してメソッドを呼び出すラムダを呼び出すことを認識しています。
これは、の引数に引数がある場合に発生することに注意しonTap
てください。onTap: _sendToDatabase
Flutterに、評価する式ではなく、保持するための参照を与えているので、これが他の場所で問題になるとは思わない(保持するための参照を取得できるようにするため)。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加