Back

[SOLVED] Catching errors with futurebuilders during api calls

  • 0
  • Flutter
ireneus
12 Feb, 2023, 14:41

wdym?

TL;DR
TL;DR: The user was having trouble catching errors with FutureBuilders during API calls. They discovered that using 'throw' or 'rethrow' instead of 'return' resolved the issue. Additionally, they needed to define the type explicitly when using FutureBuilder. The user also learned how to check for the presence of an error property in the snapshot object.
ireneus
12 Feb, 2023, 14:42

snapshot is of type object

Maniac_Fighter
12 Feb, 2023, 14:44

snapshot is basically AsyncSnapshot<YOUR_TYPE> snapshot no snapshot.data would return the data of the type you have passed and snapshot.error would give back the exception it had during calling of the function

Maniac_Fighter
12 Feb, 2023, 14:45

now the error could be of any type. In your case , it should be object or dynamic

ireneus
12 Feb, 2023, 14:46

where/how would i write checks for that there is no property error in my snapshot object

ireneus
12 Feb, 2023, 14:47

nvm figured out

ireneus
12 Feb, 2023, 14:52

@Maniac_Fighter after writing checks inside the builder the exception is still being thrown

ireneus
12 Feb, 2023, 14:53

problem comes before the builder can read the checks

Maniac_Fighter
12 Feb, 2023, 14:56

can you share your futurebuilder code ??

ireneus
12 Feb, 2023, 15:00

where snapshot is the getfie(index) passed down from widget

ireneus
12 Feb, 2023, 15:01

it doesnt even matter the exception is thrown before the code is called

Maniac_Fighter
12 Feb, 2023, 15:04

so it should show in the stackTrace where the exception is being thrown right?

ireneus
12 Feb, 2023, 15:05

yeah shown under debug vars too

ireneus
12 Feb, 2023, 15:05
ireneus
12 Feb, 2023, 15:06
Maniac_Fighter
12 Feb, 2023, 15:08

Also let's try to refactor this as well, getFile must be Future<Uint8List> Also , it needs async await Avoid using then, if you want the result, try storing it in a variable then print it

Now when you get the error, or when it reaches the catch block, its necessary, to either return a type of Uint8List or throw an exception

so instead of return forceError, use throw or rethrow Now your future builder would be able to catch it

Maniac_Fighter
12 Feb, 2023, 15:08

in the stack trace, could you see the who is calling that function??

ireneus
12 Feb, 2023, 15:10

already tried async await until i realised futurebuilder expects of type Future<Object?>? and not Future<Uint8List>

Maniac_Fighter
12 Feb, 2023, 15:11

FutureBuilder accepts any type you just need to define it explicitly

Maniac_Fighter
12 Feb, 2023, 15:11

FutureBuilder<T>

Maniac_Fighter
12 Feb, 2023, 15:12

if not types given, then it considers an object automatically

ireneus
12 Feb, 2023, 15:12

ill see what i can do thank you for your patience

ireneus
12 Feb, 2023, 15:39

i fixed it, for some reason it wouldnt use throw and rethrow so i made the types nullable and returned null at the cost of not being able to use the error property

Maniac_Fighter
12 Feb, 2023, 15:40

If it works don't touch it

ireneus
14 Feb, 2023, 10:29

[SOLVED] Catching errors with futurebuilders during api calls

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more