I am trying to get a list of my data from my Firebase Realtime Database. I am using the widget FirebaseAnimatedList to populate and correctly query the information.
While I try to reference the data snapshot, I am getting an error. For any Swift / UIKit programmers, I am trying to produce the following:
if let value = snapshot.value["name"] as? String {
print(value)
}
I tried calling the snapshot.value['name'] here. Then I got an error that I needed to support optional functionality, and when I added the ?.
, I got another error that I can't:
final FirebaseDatabase database = FirebaseDatabase.instance;
DatabaseReference _categoriesRef = FirebaseDatabase.instance
.ref()
.child("Apps")
.child(GlobalVariables.restaurantId)
.child("menu")
.child("categories");
Container(
height: 50,
child: new FirebaseAnimatedList(
scrollDirection: Axis.horizontal,
query: _categoriesRef,
padding: EdgeInsets.fromLTRB(25, 0, 25, 0),
itemBuilder: (
BuildContext context,
DataSnapshot snapshot,
Animation<double> animation,
int index,
) {
var title = snapshot.value?.["hi there"];
var titleText = "All ";
return GestureDetector(
onTap: () {
setState(() {
selectedIndex = index;
});
},
child: Container(
height: 20,
child: categoryContainer(
titleText,
"hello",
index,
),
width: getWidth(titleText) 50,
),
);
},
),
),
CodePudding user response:
The value of a snapshot can be of many types, and it seems you need to indicate the type here.
var value = Map<String, dynamic>.from(snapshot.value as Map);
var title = value["name"];
Also see: Get Map<String, dynamic> from Map<dynamic, dynamic> flutter