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,
Share:
1,066
You and the web
Author by

You and the web

Updated on December 28, 2022

Comments

  • You and the web
    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
      nvoigt over 3 years
      Why do you use a type there at all?
    • Harshvardhan Joshi
      Harshvardhan Joshi over 3 years
      I 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
      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
    nvoigt over 3 years
    Why do you think a type needs to be explicitly given there at all?
  • You and the web
    You and the web over 3 years
    Yes the dynamic work for me but i prefer to @HarshvardhanJoshi's comment by importing typed_data. Thanks for the answer.
  • You and the web
    You and the web over 3 years
    Yes i appreciate your answer. Can you please briefly explain that why we should not need <Uint8List>
  • nvoigt
    nvoigt over 3 years
    I 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 an int, you can just do method(5) instead of method<int>(5) because the compiler already inferred from your usage that you mean int. See dart.dev/guides/language/type-system#type-inference