Questions tagged [flutter-future]

71 questions
43
votes
6 answers

What is a Future and how do I use it?

I get the following error: A value of type 'Future' can't be assigned to a variable of type 'int' It might be another type instead of int, but basically the pattern is: A value of type 'Future' can't be assigned to a variable of type…
nvoigt
  • 75,013
  • 26
  • 93
  • 142
42
votes
5 answers

Flutter how to use Future return value as if variable

I want to get Future return value and use it like variable. I have this Future function Future _fetchUserInfo(String id) async { User fetchedUser; await Firestore.instance .collection('user') .document(id) …
Daibaku
  • 11,416
  • 22
  • 71
  • 108
3
votes
1 answer

Adding the Authorization header in the flutter code gives back an error response whereas the same request works fine in the postman

I am having a weird error in flutter where just adding an authorization header allows me to have an error. Current Problem I am having, postData(data, apiUrl) async { var fullUrl = _url + apiUrl; var at = await _accessToken; …
3
votes
0 answers

Flutter - how to correctly unit test FutureBuilder

I'm trying to unit test widgets inside a FutureBuilder but I can't figure it out. I've got a widget that has a FutureBuilder with a builder that returns a CameraPreview widget when the connection state is done: body: FutureBuilder( …
3
votes
1 answer

Terminate a function in dart/flutter

I have a function witch returns a Future. main(){ function().timeout(Duration(milliseconds: 100), onTimeout: () { print("exit function"); }); } Futurefunction()async{ await Future.delay(Duration(milliseconds: 500); …
David Wild
  • 193
  • 1
  • 8
2
votes
1 answer

Flutter Riverpod Future provider - requires async and await?

I've been reviewing the RiverPod 2 tutorial at https://codewithandrea.com/articles/flutter-state-management-riverpod/ In the section dealing with Future providers there is a code snippet as shown below... final weatherFutureProvider =…
user2868835
  • 1,250
  • 3
  • 19
  • 33
2
votes
3 answers

Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the future's type

I am trying to write a dart code program which can register users and sign in users, upon error print its description which i will eventually use in a Toast but it given an error on catchError Future signIn(String email, String password) async…
2
votes
1 answer

Futurebuilder snapshot has no data

CollectionReference users = FirebaseFirestore.instance.collection('Users'); FirebaseAuth auth = FirebaseAuth.instance; String uid = FirebaseAuth.instance.currentUser!.uid.toString(); var userData; var dbFuture; @override void…
2
votes
0 answers

Correct use of Future.delayed to highlight text while playing audio flutter

In my flutter application, I'm trying to change the background color of text in list items for an audio playing. So each list item has one audio file. I have to detect when the audio ends and then make the next item highlighted accordingly and play…
Noor
  • 193
  • 1
  • 2
  • 15
2
votes
1 answer

Why snapshot inside Future.Builder has never errors?

What's the right way to generate a snapshot.error for a FutureBuilder? I've a singleton to handle http requests, with a method: try { var response = await http.get(url); // Success if (response.statusCode == 200) { [...] } //…
Jumpa
  • 4,319
  • 11
  • 52
  • 100
2
votes
2 answers

Flutter Async Load Multiple Functions One After The Other

How can I let Flutter know to wait for one function (and therefore the future) to complete, until the next function is called? It seems like the load() function itself completes before the loadFeed() Future is finished, how can I fix that? Here is…
Leo D.
  • 73
  • 8
1
vote
1 answer

Future Builder not rendering data table

in want create table from api response.i created data table using future builder. The future builder fires api twice not not return any data class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State
Hari .S
  • 59
  • 6
1
vote
1 answer

How to cancel the then function in a future if catcherror is executed in flutter app

I'm trying to login in users using a third party api. But the problem is whenever an error occurs and the catch error is executed the "then" function that holds the navigation to the HomeScreen also is executed. Please is there a way to login user…
Ariyo
  • 49
  • 4
1
vote
1 answer

FutureBuilder snapshot is empty but Future has correct data AND FutureBuilder constantly refreshing - Flutter

I have screens in a TabBarView that share data so I decided to wrap the TabBarView in a FutureBuilder and pass the data from that into both screens. The Future is called fetchUsersAndPosts() and it is meant to return [[*users], [*posts*]] but the…
1
vote
1 answer

Correct way to load ListView data source initially

I have a stateful widget whose state builds a ListView. The ListView gets its data from an http API. I am using a Future method called getData to retrieve this data and populate a List<> with it before calling setState. My question is where…
barry
  • 4,037
  • 6
  • 41
  • 68
1
2 3 4 5