I am trying to load a image from the memory and, my method is giving Future<Uint8List> but the Image.memory want Uint8List in flutter
this was the error message
The argument type Future<Uint8List> can't be assigned to the parameter type `Uint8List
Here I am using image
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image_puzzle/function.dart';
import 'package:image_puzzle/getimages.dart';
import 'widgets.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool gotImage = false;
Future<Uint8List> downloadImage() async {
var readWriteObj = ReadAndWrite();
var imgData = await readWriteObj.getImages();
readWriteObj
.writeDataToFile('image1', imgData)
.then((value) => {print('Image Saved ')});
return imgData;
}
readImage() async {
var image = ReadAndWrite().
writeDataToFile('image1', await ReadAndWrite().getImages());
return image;
}
double aopacity = 1;
double aopacity1 = 1;
double aopacity2 = 1;
double aopacity3 = 1;
double aopacity4 = 1;
double aopacity5 = 1;
double aopacity6 = 1;
double aopacity7 = 1;
double aopacity8 = 1;
@override
void initState() {
downloadImage();
// readImage();
super.initState();
}
@override
Widget build(BuildContext context) {
List<BrainOfApp> questionList = [];
int listNumber = 0;
questionList.add(BrainOfApp('salman',
// 'https://cdn.pixabay.com/photo/2020/11/16/04/41/salman-khan-5747949_960_720.png',
['elon musk', 'sachin', 'salman', 'mahi']));
return Scaffold(
appBar: AppBar(
title: const Text('Bollywood Quiz'),
backgroundColor: Colors.black,
),
body: Stack(children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Center(
child: Stack(
fit: StackFit.expand,
children: [
Image.memory(downloadImage()),
Column(
children: [
Expanded(
child: Row(
children: [
CoverTile(aopacity),
CoverTile(aopacity1),
CoverTile(aopacity2)
],
),
),
Expanded(
child: Row(
children: [
CoverTile(aopacity3),
CoverTile(aopacity4),
CoverTile(aopacity5),
],
),
),
Expanded(
child: Row(
children: [
CoverTile(aopacity6),
CoverTile(aopacity7),
CoverTile(aopacity8),
],
),
)
],
),
],
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
width: 20,
),
Expanded(
child: ElevatedButton(
child: Text(
questionList[listNumber].options![0].toUpperCase(),
style: const TextStyle(fontSize: 24),
),
style: ButtonStyle(
foregroundColor:
MaterialStateProperty.all<Color>(Colors.white),
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
shape:
MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: const BorderSide(color: Colors.white70),
),
),
),
onPressed: () {
listNumber++;
}),
),
const SizedBox(
width: 60,
),
Expanded(
child: ElevatedButton(
child: Text(
questionList[listNumber].options![1].toUpperCase(),
style: const TextStyle(fontSize: 24),
),
style: ButtonStyle(
foregroundColor:
MaterialStateProperty.all<Color>(Colors.white),
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
shape:
MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: const BorderSide(color: Colors.white70),
),
),
),
onPressed: () {
listNumber++;
}),
),
const SizedBox(
width: 20,
),
],
),
const SizedBox(height: 60),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
width: 20,
),
Expanded(
child: ElevatedButton(
child: Text(
questionList[listNumber].options![2].toUpperCase(),
style: const TextStyle(fontSize: 24),
),
style: ButtonStyle(
foregroundColor:
MaterialStateProperty.all<Color>(Colors.white),
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
shape:
MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: const BorderSide(color: Colors.white70),
),
),
),
onPressed: () {
listNumber++;
}),
),
const SizedBox(
width: 60,
),
Expanded(
child: ElevatedButton(
child: Text(
questionList[listNumber].options![3].toUpperCase(),
style: const TextStyle(fontSize: 24),
),
style: ButtonStyle(
foregroundColor:
MaterialStateProperty.all<Color>(Colors.white),
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
shape:
MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: const BorderSide(color: Colors.white70),
),
),
),
onPressed: () {
listNumber++;
}),
),
const SizedBox(
width: 20,
),
],
),
const SizedBox(height: 60),
],
)
]));
}
}
This is read and write class
import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart';
import 'package:path_provider/path_provider.dart';
class ReadAndWrite {
Future<Uint8List> getImages() async {
var url = Uri.parse(
'https://cdn.pixabay.com/photo/2020/11/16/04/41/salman-khan-5747949_960_720.png');
Response response = await get(url);
var bytes = response.bodyBytes;
return bytes;
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
print(directory.path);
return directory.path;
}
Future<File> getLocalFile(String fileName) async {
final localPath = await _localPath;
return File('$localPath/$fileName');
}
Future<File> writeDataToFile(String fileName, Uint8List data) async {
final file = await getLocalFile(fileName);
return file.writeAsBytes(data);
}
Future<Uint8List> readFromFile(String fileName) async {
final file = await getLocalFile(fileName);
return file.readAsBytes();
}
}
The problem is that,I'm trying to load the image from memory but,
my method is giving bytes in future but Image.memory widget is accepting that in future
I can't use await in that widget too.