我正在尝试从内存中加载图像,我的方法是给出Future<Uint8List>
但Image.memory
需要Uint8List
颤抖
这是错误消息参数类型Future<Uint8List>
不能分配给参数类型`Uint8List
我在这里使用图像
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),
],
)
]));
}
}
这是读写类
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();
}
}
问题是,我正在尝试从内存中加载图像,但是,我的方法将来会给出字节,但是Image.memory
小部件将来会接受
我也不能在那个小部件中使用 await 。