Flutter using Uint8List in FutureBuilder is giving error
1,066
Solution 1
try this solution:
FutureBuilder<Uint8List>
to
FutureBuilder<dynamic>
or
FutureBuilder<List<int>>
Solution 2
Just leave out the type, the compiler is smart enough to infer that from the type of Future<>
you provide:
FutureBuilder(
future: asset.thumbData,
Author by
You and the web
Updated on December 28, 2022Comments
-
You and the web over 1 year
I'm building a flutter gallery app from this reference Flutter Image Gallery
All works fine but I'm getting error inside
Future Builder
This is the error warning I'm getting
The name 'Uint8List' isn't a type so it can't be used as a type argument. Try correcting the name to an existing type, or defining a type named 'Uint8List'.
And this is the runtime error I'm getting
Error: 'Uint8List' isn't a type. return FutureBuilder<Uint8List>( ^^^^^^^^^
This
Uint8List
is totally new concept for me, don't know what to do.This is the widget that returns a Future Builder
@override Widget build(BuildContext context) { // We're using a FutureBuilder since thumbData is a future return FutureBuilder<Uint8List>( future: asset.thumbData, builder: (_, snapshot) { final bytes = snapshot.data; // If we have no data, display a spinner if (bytes == null) return CircularProgressIndicator(); // If there's data, display it as an image return InkWell( onTap: () { // TODO: navigate to Image/Video screen }, child: Stack( children: [ // Wrap the image in a Positioned.fill to fill the space Positioned.fill( child: Image.memory(bytes, fit: BoxFit.cover), ), // Display a Play icon if the asset is a video if (asset.type == AssetType.video) Center( child: Container( color: Colors.blue, child: Icon( Icons.play_arrow, color: Colors.white, ), ), ), ], ), ); }, ); }
I've search it on google but doesn't get any satisfied answer. Thanks in advance.
-
nvoigt over 3 yearsWhy do you use a type there at all?
-
Harshvardhan Joshi over 3 yearsI think the import is missing from your code, add this (package)[pub.dev/packages/typed_data] in the pubspec & try again by importing the (typed_data)[pub.dev/documentation/typed_data/latest].
-
You and the web over 3 years@nvoigt I just refer to an article.
It returns a Future<Uint8List> representing the actual bytes of the thumbnail
-
-
nvoigt over 3 yearsWhy do you think a type needs to be explicitly given there at all?
-
You and the web over 3 yearsYes the dynamic work for me but i prefer to @HarshvardhanJoshi's comment by importing typed_data. Thanks for the answer.
-
You and the web over 3 yearsYes i appreciate your answer. Can you please briefly explain that why we should not need
<Uint8List>
-
nvoigt over 3 yearsI indirectly already did. It is called "type inference", most languages can do that. If you have for example a method
method<T>(T t)
, then when you pass in anint
, you can just domethod(5)
instead ofmethod<int>(5)
because the compiler already inferred from your usage that you meanint
. See dart.dev/guides/language/type-system#type-inference