Is there any package or method available to save/download image from URL in flutter for web as well app.
You can use Firebase storage or s3-Cognito to achieve this. First, you need to upload the image to the Database using file picker or image picker and use the getDownloadUrl() method to get the imageUrl
You don't need a package.
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'dart:typed_data';
TextButton(
onPressed: () async {
String imageUrl='https://......example';
Uint8List response = await http.get(Uri.parse(imageUrl)).then(
(value) => value.bodyBytes);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Image.memory(response),
);
},
);
},
child: Text(
'Show Image from memory',
));
This code alone works without any packages. Now that you have it in memory, you can store it and do other things with it. Convert it between base64 and Unit8List.
Related
I'm using this template/package to draw in Flutter https://pub.dev/packages/scribble/example.
Now I want to convert the drawn image to a File. So that I can use it in my tflite model. To predict the image the runModelOnImage (tflite package) function requires a path to the image.
The used scribble package, provides the saveImage function to return the drawn image as byteData:
// saveImage
Future<void> _saveImage(BuildContext context) async {
final image = await notifier.renderImage();
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Your Image"),
content: Image.memory(image.buffer.asUint8List()),
),
);
}
How can I transform the byteData image to a File so I can use it in my model?
ByteData is an abstraction for:
A fixed-length, random-access sequence of bytes that also provides random and unaligned access to the fixed-width integers and floating point numbers represented by those bytes.
As Gunter mentioned in the comments, you can use File.writeAsBytes. It does require a bit of API work to get from ByteData to a List, however.
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
Future<void> writeToFile(ByteData data, String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
You can read more about this on here
I'm using the pdf package (link to pub.dev page) to create documents inside my app.
How can I save the document from the preview page?
I know about the built-in print and share functions, but I need something else.
I need a function that does the following:
save the document to the device
upload it to Firebase Storage
delete the local file
I know how to do all of these steps, but I'm unable to access the Uint8List bytes of the pdf document that is being previewed... I've tried to add a PdfPreviewAction, but I still can't access the bytes of the document from it
all you have to do is call the save method in the Document() object
import "package:pdf/widgets.dart" as w;
final pdf = w.Document();
Future<Uint8List> getPDFData() async {
return await pdf.save();
}
you can do all the operations using the getPDFData() method
for example:
return PdfPreview(
build: (format) => getPdfData(),
actions: [
PdfPreviewAction(
icon: Icon(Icons.print),
onPressed: (context, fn, format) {
//if you call fn(format), you'll get Future<UInt8List>
}),
],
);
Important: I have searched a lot but did not find any solution that I can understand.
I am using photo manager to show images and then after selecting an image I want to navigate to the next screen and show that image there (later I want to upload that image to firebase storage).
So the type of File I am getting from the photo_manager package is Future < File > and I am receiving File from the second screen.
How can I convert future to file?
First Screen:
IconButton(
icon: const Icon(Icons.arrow_forward, color: primaryColor),
onPressed: () {
Get.to(PostDetail(
imgFile:
_controller.mediaList[_controller.selectedImageIndex].file,
));
},
),
Second Screen:
On the second screen, I'm getting File.
final File imgFile;
const PostDetail({this.imgFile});
Source Code:: Add Post Page
So as described in photo_manager docs, you need to get the file from the AssetEntity list before moving to the next page.
So on button pressed to go to next page get the file and then pass it to the Page
Below code should work:
IconButton(
icon: const Icon(Icons.arrow_forward, color: primaryColor),
onPressed: () async {
File file = await _controller.mediaList[_controller.selectedImageIndex].file; // image file
Get.to(PostDetail(
imgFile: file,
));
},
),
File and Future<File> are 2 different things. the File is a variable that its value is in the present. but Future<File> as its name suggests, its value exist in the Future which means its value takes some time to reach you.
for example, if you send a request to a webserver, the response takes some time to reach you. so, in that case the response is a Future.
in your case the image you are trying to get is supplied in a way that it takes time. to get the File from Future<File> you have to wait. So before sending the file to this Screen, use an async and wait to turn the Future<File> to File. (i dont know how you get the file)
for example,
File myFile = await getFileorWhaterver();
Your _controller.mediaList[_controller.selectedImageIndex].file is a Future<File> means that the value of the File is promised to be returned sometimes in the future. This usually because you are doing a network call asynchronously to get the data from API.
In order to convert it to File, you need to await till the moment that value is available. However you cannot do an async/await within the UI, that's why Flutter provide a widget named FutureBuilder to wait for a certain Future value, then let you use that value in the UI.
In your case, you can do this:
FutureBuilder(
future: _controller.mediaList[_controller.selectedImageIndex].file,
builder: (context, snapshot) {
return IconButton(
icon: const Icon(Icons.arrow_forward, color: primaryColor),
onPressed: () {
if (snapshot.hasData)
Get.to(PostDetail(imgFile: snapshot.data));
},
);
})
I'm learning to make an application that captures the response api from the server and then I want to make the response from the api into a pdf file in flutter, are there any instructions or examples of how I should make it? thanks for help...
You can use the response that you get from the API into a PDF file using this package: pdf
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
final pdf = pw.Document();
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child: pw.Text(<API response>),
);
}));
final file = File("example.pdf");
await file.writeAsBytes(pdf.save());
Anyone knows how to add tutorial when the app is launched for the first time in flutter? Example is attached as an image.
You can use tutorial_coach_mark library,like:
import 'package:flutter/material.dart';
import 'package:tutorial_coach_mark/tutorial_coach_mark.dart';
void showTutorial() {
TutorialCoachMark(
context,
targets: targets, // List<TargetFocus>
colorShadow: Colors.red, // DEFAULT Colors.black
// alignSkip: Alignment.bottomRight,
// textSkip: "SKIP",
// paddingFocus: 10,
finish: (){
print("finish");
},
clickTarget: (target){
print(target);
},
clickSkip: (){
print("skip");
}
)..show();
}
To be able to use it for the first time you need the shared_preferences package:
SharedPreferences prefs = await SharedPreferences.getInstance();
var watchedIntro=prefs.getBool('watchedIntro')??false;
if(!watchedIntro)
and when the tutorial ended set watchedIntro to true:
await prefs.setBool('watchedIntro', true);
You can try out this package, tutorial_coach_mark.