0

I'm new to Flutter and trying to load data from Firestore and then use the data in my FutureBuilder. I have to load the data before I can build the UI because how I build it depends on the data.

So I get the data with an async function I wrote called .getUserfromFirestore, which returns an AppUser object containing all my data mapped from the Firestore data. I confirmed with debug prints that the AppUser has all the data.

But the object I get back is considered a Future version of the object instead of the actual object with its data. So when I try to debugPrint(${appUser.currentTimeline}), for example, I get the error:

The getter 'currentTimeline' isn't defined for the type Future<AppUser> 

How can I access the actual AppUser object returned from my async function?

Here is my code:

class _HomePageState extends State<HomePage> { late Future<AppUser?> appUser; //appUser is an object with many parameters. @override initState() { super.initState(); // this gets the data and makes an appUser object appUser = UserService().getUserfromFirestore(); } // This is the basic structure of the page. @override Widget build(BuildContext context) { return FutureBuilder( // Using a FutureBuilder here because we have to wait to pull the user data from Firestore... future: appUser, // Must wait for appUser to proceed with the build... builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { debugPrint("The AsyncSnapshot has data."); debugPrint('${appUser.currentTimeline}'); } return ColorfulSafeArea( <this builds the UI>) } 
2
  • The data is in snapshot. I think it would be snapshot.data.currentTimeline. Commented Dec 8, 2024 at 21:54
  • @RandalSchwartz. Yep. I knew it had to be easy, but I spent hours trying to figure it out and I did not see that the snapshot had the data anywhere in the documentation. Only certain data in the snapshot could be used to manage the waiting. Thanks! If you want to put an answer I'll mark it as correct. Commented Dec 8, 2024 at 22:12

1 Answer 1

1

The data is in snapshot. Try looking at snapshot.data.currentTimeline.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.