flutterfire
flutterfire copied to clipboard
[cloud_firestore] Write to Firestore onSave
Am trying to create a group and on save, i want to add it to database using firebase. But the save is not successful. Here is my code.
ConversationMenu({Key key, this.group, this.reciever}) : super(key: key);
final Group group;
final User reciever;
@override
_ConversationMenuState createState() =>
_ConversationMenuState(group: group, reciever: reciever);
}
class _ConversationMenuState extends State<ConversationMenu> {
_ConversationMenuState({Key key, this.group, this.reciever});
Group group;
final User reciever;
User currentUser;
bool groupMessage;
final FirebaseService updateGroup = FirebaseService();
@override
void initState() {
groupMessage = (group != null);
_getUser();
super.initState();
}
void _getUser() async {
FirebaseAuth.instance.currentUser().then((FirebaseUser user) {
Firestore.instance
.collection('users')
.document(user.uid)
.get()
.then((DocumentSnapshot ds) {
setState(() {
this.currentUser = User.fromDocument(ds);
});
});
});
}
TextEditingController editingController = TextEditingController();
String grabText = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: Color(0xffffffff),
iconTheme: IconThemeData(color: Color(0xff000000)),
title: Text("Details",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Color(0xff000000),
fontSize: 18),
maxLines: 1,
textAlign: TextAlign.center),
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
tooltip: 'Back',
onPressed: () => Navigator.pop(context),
),
),
backgroundColor: Colors.white,
body: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
groupMessage
? SizedBox(
height: 80,
child: Row(children: <Widget>[
Text("Group Name",
style: TextStyle(fontWeight: FontWeight.bold)),
new Flexible(
child: TextField(
controller: editingController,
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(10.0),
hintText: group != null ? group.name : "Add Name",
hintStyle: new TextStyle(color: Colors.grey),
),
),
),
SizedBox(
width: 90,
child: editingController.text.length > 0
? OutlineButton(
onPressed: () {
// grabText = editingController.text;
// group = json.decode(grabText);
// updateGroup.updateGroupConversation(group);
Firestore.instance
.collection("groups")
.document(group.name)
.updateData({
'name': grabText,
});
},
child: Text("Save"),
)
: Text("")),
]))
: Text(""),
groupMessage ? Divider() : Text(""),
groupMessage
? SizedBox(
height: 20,
)
: Text(""),
Padding(
padding: EdgeInsets.all(10),
child:
Text("Members", style: TextStyle(fontWeight: FontWeight.w100)),
),
Expanded(
child: BaseView<UserViewModel>(builder: (context, child, model) {
return _getBodyUi(context, model);
}),
),
// Divider(),
// ListTile(
// title: Text("Block",
// style:
// TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
// ),
Divider(),
ListTile(
title: Text("Report",
style:
TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
onTap: () async {
String url =
'https://triblnetwork.atlassian.net/servicedesk/customer/portals';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
},
),
Divider()
],
),
);
}
Widget _getListUi(UserViewModel model) {
List<User> users = [];
if (groupMessage) {
group.users.forEach((userId) {
if (model.userDict[userId] != null) {
users.add(model.userDict[userId]);
}
});
} else {
users.add(reciever);
}
return ListView.builder(
itemCount: users == null ? 0 : users.length,
itemBuilder: (context, index) {
return UserItem(
user: users[index],
onClicked: (user) {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => Profile(
user: user,
type: currentUser.id == user.id
? ProfileType.mine
: ProfileType.other,
)));
});
});
}
Widget _getBodyUi(BuildContext context, UserViewModel model) {
switch (model.state) {
case ViewState.Busy:
return _getLoadingUi(context);
case ViewState.NoDataAvailable:
return _noDataUi(context, model);
case ViewState.Error:
return _errorUi(context, model);
case ViewState.DataFetched:
default:
return _getListUi(model);
}
}
Widget _getLoadingUi(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Theme.of(context).primaryColor)),
Text('Fetching data ...')
],
));
}
Widget _noDataUi(BuildContext context, UserViewModel model) {
return _getCenteredViewMessage(context, "No data available yet", model);
}
Widget _errorUi(BuildContext context, UserViewModel model) {
return _getCenteredViewMessage(
context, "Error retrieving your data. Tap to try again", model,
error: true);
}
Widget _getCenteredViewMessage(
BuildContext context, String message, UserViewModel model,
{bool error = false}) {
return Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
Text(
message,
// style: viewErrorTitle,
textAlign: TextAlign.center,
),
error
? Icon(
// WWrap in gesture detector and call you refresh future here
Icons.refresh,
color: Colors.white,
size: 45.0,
)
: Container()
])));
}
}```
Hi @uimarshall I suggest you to post questions specific to your code on Stack Overflow You may also check our guides. Closing, as this isn't an issue with FlutterFire itself, if you disagree please write in the comments and I will reopen it Thank you