I am trying to insert to a SQFlite database a webp image that I have in my assets.
But I don't know how to convert the asset image to a Uint8List which is the data type in my DB.
How can I do it?
I have tried this:
Future<Uint8List> convert() async {
final ByteData bytes = await rootBundle.load('assets/ab.webp');
final Uint8List list = bytes.buffer.asUint8List();
return list;
}
Uint8List list = convert();
But I get the following error:
Type: Future Function()
A value of type 'Future' can't be assigned to a variable of type 'Uint8List'.
Try changing the type of the variable, or casting the right-hand type to 'Uint8List'.
Thank you in advance
convert() is a async function so when you want to use it you should await for the result and also your convert function does not rerturn any thing.
try this:
Future<Uint8List> convert() async {
final ByteData bytes = await rootBundle.load('assets/image.webp');
final Uint8List list = bytes.buffer.asUint8List();
return list;
}
then use it like this:
Uint8List list = await convert();
Related
I'm trying to upload image from flutter web app to firebase storage but this error occur in the code.
The argument type 'Uint8List?' can't be assigned to the parameter type 'Uint8List'
How i can resolve this error?
Code is given below:
Future<void> pickImage() async
{
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
Uint8List? fileBytes =result.files.first.bytes;
String fileName = result.files.first.name;
setState(() {
fname=fileName;
});
// Upload file
await FirebaseStorage.instance.ref('images/$fileName').putData(fileBytes);
var Downloadurl=await FirebaseStorage.instance.ref('images/$fileName').getDownloadURL();
setState(() {
imgUrl=Downloadurl;
});
}
}
I tried html input element and image picker package too, but cant solve the problem.
if you're sure that the fileBytes is not null, you can cast it to Uint8List like this:
await FirebaseStorage.instance.ref('images/$fileName').putData(fileBytes as Uint8List);
I have the below code that is querying artwork from https://pub.dev/packages/on_audio_query
It is able to get the artwork in Future<Uint8List?> formart. How can I convert that to Uint8List?
Future<Uint8List?> getArtWork(id)async{
return await _audioQuery.queryArtwork(id,ArtworkType.AUDIO);
}
Uint8List uint8List = getArtWork(metadata.id.toString())
use await to get the Uint8List?
Uint8List? uint8List = await getArtWork(metadata.id.toString())
Your getArtWork can return null and uint8List variable doesnt accept nullable data.
You can make it nullable
youMethod() async{
Uint8List? uint8List = await getArtWork(metadata.id.toString());
}
Here is my code below.
Future<List<int>> getImage() async {
List<int> bytes = [];
CapabilityProfile profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm80, profile);
final ByteData data = await rootBundle.load('assets/logo.png');
final buffer = data.buffer;
final image = base64.encode(Uint8List.view(buffer));
bytes += generator.image(image);
return bytes;
}
At
bytes += generator.image(image);
the error said that
Error: The argument type 'String' can't be assigned to the parameter type 'Image'.
from The docs
you need to pass the bytes not the base64
it looks like you miss decodeImage()
Future<List<int>> getImage() async {
List<int> bytes = [];
CapabilityProfile profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm80, profile);
final ByteData data = await rootBundle.load('assets/logo.png');
final Uint8List buffer = data.buffer.asUint8List();
final Image image = decodeImage(buffer);
bytes += generator.image(image);
return bytes;
}
I'm trying to read a json file's components using the following method:
import 'dart:io';
class CharacterDataReader {
Future<String> read() async {
final file = File('assets/data/character_data.json');
String data = await file.readAsString();
return data;
}
}
Now, I'm trying to assign the read values to a String named data and json.decode() it in another class using the following:
Future<String> data = CharacterDataReader().read();
Map<String, dynamic> characterData = json.decode(data);
However, this doesn't work since json.decode() only accepts Strings as a parameter. Therefore, can someone please tell me how do I convert this Future into an actual string?
since its a future you have to add await keyword
String data= await CharacterDataReader().read();
check out dart official doc on asynchronous programming
I have a form, in that form I have a signature pad. I used signature 3.2.0 package. That package consist a method toImage(). I want to store that image in the firebase storage. when I try below code.
fileImage = _controller.toImage() as File;
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putFile(fileImage);
task.onComplete.then((snapshot) async{
loadData.setSignatureURL = await snapshot.ref.getDownloadURL();
});
loadData.storeDetails();
I got an error type 'Image' is not a subtype of type 'File' in type cast.
How can I store signature as an image/
Casting can't be done on a whim, which is why you get this error.
The Image class provides a toByteData method that allows you to retrieve the raw image data as a ByteData object. You can then convert this to a Uint8List. This list then can be directly used for firebase storage with the putData method instead of putFile.
var image = await _controller.toImage();
ByteData data = await image.toByteData();
Uint8List listData = data.buffer.asUint8List();
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putData(listData);
...
If you need to encode this image to a specific type. You can use a version of the following code which encodes to JPG. It uses the image package which needs to be added as a dependency
import 'package:image/image.dart' as encoder;//This import needs to be added in the file this is being done
var image = await _controller.toImage();
//Store image dimensions for later
int height = image.height;
int width = image.width;
ByteData data = await image.toByteData();
Uint8List listData = data.buffer.asUint8List();
encoder.Image toEncodeImage = encoder.Image.fromBytes(width, height, listData);
encoder.JpegEncoder jpgEncoder = encoder.JpegEncoder();
List<int> encodedImage = jpgEncoder.encodeImage(toEncodeImage);
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putData(Uint8List.fromList(encodedImage));
...