I have used a multi-image picker and when the user selected the image I am storing the image path in the firebase. 1 or 2 paths are stored normally but when I send a path of more than 2 images it showed an exception and doesn't save the image.
below is the code of the multi-image picker which is working -:
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:fun1/Components/Rounded_Button.dart';
import 'package:fun1/Register_Options_Screens/RecoveryEmail.dart';
import 'package:fun1/Components/FabButton.dart';
import 'package:image_picker/image_picker.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:io';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'dart:async';
import 'package:flutter_absolute_path/flutter_absolute_path.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class PhotoSelectPage extends StatefulWidget {
@override
_PhotoSelectPageState createState() => _PhotoSelectPageState();
}
class _PhotoSelectPageState extends State<PhotoSelectPage> {
String selectedvalue;
bool _isvisible = false;
String _path;
//==========================================================================image picker and store
List<Asset> images = List<Asset>();
List files = [];
List resultList = [];
var data;
// List<Asset> resultList;
String _error = 'No Error Dectected';
@override
void initState() {
super.initState();
}
//============================================================to show image in a grid view
Widget buildGridView() {
if (images != null) {
return GridView.count(
crossAxisCount: 3,
children: List.generate(images.length, (index) {
Asset asset = images[index];
print(asset.getByteData(quality: 100));
return Padding(
padding: EdgeInsets.all(8),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20)),
child: AssetThumb(
asset: asset,
width: 300,
height: 300,
),
),
);
}),
);
}
else{
return Container();
}
}
//============================================================to load multiple image from assets
Future<void> loadAssets() async {
List<Asset> resultList = List<Asset>();
String error = 'No Error Dectected';
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 6,
enableCamera: true,
selectedAssets: images,
cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
materialOptions: MaterialOptions(
actionBarColor: "#abcdef",
actionBarTitle: "Choose Image",
allViewTitle: "All Photos",
useDetailsView: false,
selectCircleStrokeColor: "#000000",
),
);
} on Exception catch (e) {
error = e.toString();
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
images = resultList;
_error = error;
});
}
//===============================================================convert image to file and get the path
getImagePathFromAsset(String path) async{
final file = File(path);
return file;
}
//=============================================================get and send the file to the backend
_submit() async {
for (int i = 0; i < images.length; i++) {
var path2 =
await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
var file = await getImagePathFromAsset(path2);
var base64Image = base64Encode(file.readAsBytesSync());
files.add(base64Image);
data = {
'files': files,
};
print(data);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => RecovermailScreen()));
FirebaseFirestore firestore = FirebaseFirestore.instance;
var firebaseUser = FirebaseAuth.instance.currentUser;
firestore.collection('pal').doc('111445').set(
{
'name' : data,
}).then((_){
print("success!");
});
// print(firebaseUser);
}
}
//====================================================================================global key declaration
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
backgroundColor: Colors.deepOrange[400],
body: Container(
child: Padding(
padding: EdgeInsets.only(left: 30, right: 30, top: 15),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
height: 90,
child: ListView.builder(
itemCount: 1,
itemBuilder: (context, index) {
return Column(
children: [
buildList('Camera Roll'),
],
);
},
),
),
images.isEmpty ? Container(height: 0,) :
Container(
height: MediaQuery.of(context).size.height/5,
child: buildGridView(),
),
Padding(
padding: EdgeInsets.only(top: 20, bottom: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
images.isEmpty ? Container() :
FabButton(
icon: Icons.arrow_forward_ios_outlined,
Iconcolor: Colors.deepOrange,
onpresses: () async{
_submit();
},
),
],
),
),
],
),
),
),
),
);
}
}
and below is the error that I am getting when I send more than 2 file paths in the database.
Flutter run key commands.
h Repeat this help message.
c Clear the screen
q Quit (terminate the application on the device).
I/flutter (22073): Instance of 'Future<ByteData>'
I/flutter (22073): Instance of 'Future<ByteData>'
I/flutter (22073): {files: [/9j/4VYLRXhpZgAASUkqAAgAAAARAA4BAgAgAAAA2gAAAA8BAgAgAAAA+gAAABABAgAgAAAAGgEAABIBAwABAAAAAQAAABoBBQABAAAAOgEAABsBBQABAAAAQgEAACgB
AwABAAAAAgAAADEBAgAgAAAASgEAADIBAgAUAAAAagEAABMCAwABAAAAAgAAACACBAABAAAAAAAAACECBAABAAAAAAAAACICBAABAAAAAAAAACMCBAABAAAAAAAAACQCBAABAAAAAQAAACUCAgAgAAAAfgEA
AGmHBAABAAAAngEAAFAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEhUQwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASFRDIERlc2lyZSA3MjggZHVhbCBzaW0AAAAAAAAAAABIAAAA
AQAAAEgAAAABAAAATWVkaWFUZWsgQ2FtZXJhIEFwcGxpY2F0aW9uCgAAAAAyMDE4OjExOjA3IDIwOjQ3OjQwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHQCaggUAAQAAAAADAACdggUAAQAA
AAgDAAAiiAMAAQAAAAAAAAAniAMAAQAAAPQBAAAAkAcABAAAADAyMjADkAIAFAAAABADAAAEkAIAFAAAACQDAAABkQcABAAAAAECAwAEkgoAAQAAADgDAAAHkgMAAQAAAAIAAAAIkgMAAQAAAP8AAAAJkgMA
AQAAAAAAAAAKkgUAAQAAAEADAAB8kgcAAAIAAEgDAACQkgIAAgAAADUzAACRkgIAAgAAADUzAACSkgIAAgAAADUzAAAAoAcABAAAADAxMDABoAMAAQAAAAEAAAACoAQAAQAAAAAKAAADoAQAAQAAAKAFAAAF
oAQAAQAAAMYFAAACpAMAAQAAAAAAAAADpAMAAQAAAAAAAAAEpAUAAQAAAEgF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: [/9j/4VYLRXhpZgAASUkqAAgAAAARAA4BAgAgAAAA2gAAAA8BAgAgAAAA+gAAABABAgAgAAAAGgEAABIBAwABAAAAAQAAABoBBQABAAAAOgEAABsBBQABAAAAQgEAACgB
AwABAAAAAgAAADEBAgAgAAAASgEAADIBAgAUAAAAagEAABMCAwABAAAAAgAAACACBAABAAAAAAAAACECBAABAAAAAAAAACICBAABAAAAAAAAACMCBAABAAAAAAAAACQCBAABAAAAAQAAACUCAgAgAAAAfgEA
AGmHBAABAAAAngEAAFAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEhUQwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASFRDIERlc2lyZSA3MjggZHVhbCBzaW0AAAAAAAAAAABIAAAA
AQAAAEgAAAABAAAATWVkaWFUZWsgQ2FtZXJhIEFwcGxpY2F0aW9uCgAAAAAyMDE4OjExOjA3IDIwOjQ3OjQwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHQCaggUAAQAAAAADAACdggUAAQAA
AAgDAAAiiAMAAQAAAAAAAAAniAMAAQAAAPQBAAAAkAcABAAAADAyMjADkAIAFAAAABADAAAEkAIAFAAAACQDAAABkQcABAAAAAECAwAEkgoAAQAAADgDAAAHkgMAAQAAAAIAAAAIkgMAAQAAAP8AAAAJkgMA
AQAAAAAAAAAKkgUAAQAAAEADAAB8kgcAAAIAAEgDAACQkgIAAgAAADUzAACRkgIAAgAAADUzAACSkgIAAgAAADUzAAAAoAcABAAAADAxMDABoAMAAQAAAAEAAAACoAQAAQAAAAAKAAADoAQAAQAAAKAFAAAF
oAQAAQAAAMYFAAACpAMAAQAAAAAAAAADpAMAAQAAAAAAAAAEpAUAAQAAAEgFAAAGpAMAAQAAAAAAAAAIpAMAAQAAAAAAAAAJpAMAAQAAAA
E/flutter (22073): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [cloud_firestore/invalid-argument] Client specified an invalid argument
. Note that this differs from failed-precondition. invalid-argument indicates arguments that are problematic regardless of the state of the system (e.g., an
invalid field name).
E/flutter (22073): #0 MethodChannelDocumentReference.set (package:cloud_firestore_platform_interface/src/method_channel/method_channel_document_referen
ce.dart:43)
E/flutter (22073): <asynchronous suspension>
E/flutter (22073): #1 _PhotoSelectPageState._submit.<anonymous closure> (package:hitch_fun1/Register_Options_Screens/PhotoSelect_Screen.dart:151)
E/flutter (22073): <asynchronous suspension>
E/flutter (22073):
E/flutter (22073): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [cloud_firestore/invalid-argument] Client specified an invalid argument
. Note that this differs from failed-precondition. invalid-argument indicates arguments that are problematic regardless of the state of the system (e.g., an
invalid field name).
E/flutter (22073): #0 MethodChannelDocumentReference.set (package:cloud_firestore_platform_interface/src/method_channel/method_channel_document_referen
ce.dart:43)
E/flutter (22073): <asynchronous suspension>
E/flutt