SQFLite DataBase Not Created - flutter

I have tried everything I know But Not been able to solve the issue
RUN Output :
Launching lib\main.dart on moto g 40 fusion in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
D/FlutterLocationService(30742): Creating service.
D/FlutterLocationService(30742): Binding to location service.
Debug service listening on ws://127.0.0.1:51746/KGrWp8utBGI=/ws
Syncing files to device moto g 40 fusion...
E/SQLiteLog(30742): (1) no such table: placestore in "SELECT * FROM placestore"
I/AssistStructure(30742): Flattened final assist data: 396 bytes, containing 1 windows, 3 views
D/MediaScannerConnection(30742): Scanned /data/user/0/com.example.memory_place/cache/04c048c7-d58f-497c-b050-fb56e943fc1b615751689670497232.mp4 to null
D/ThumbnailPlugin(30742): original w:1080, h:1920 => 150, 267
E/flutter (30742): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Cannot copy file to '/data/user/0/com.example.memory_place/app_flutter/�$��� 5�5f��O ���
======== Exception caught by gesture ===============================================================
The following LateError was thrown while handling a gesture:
LateInitializationError: Field '_CurrentThumbnail#643317077' has not been initialized.
When the exception was thrown, this was the stack:
#0 AddScreen._CurrentThumbnail (package:memory_place/Screens/add_places_screen.dart)
#1 AddScreen.build.SafePlace (package:memory_place/Screens/add_places_screen.dart:25:47)
#2 AddScreen.build. (package:memory_place/Screens/add_places_screen.dart:64:15)
#3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
#4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
#5 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
#6 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
#7 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:230:7)
#8 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:563:9)
#9 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:94:12)
#10 PointerRouter._dispatchEventToRoutes. (package:flutter/src/gestures/pointer_router.dart:139:9)
#11 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
#12 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:137:18)
#13 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:123:7)
#14 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:439:19)
#15 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
#16 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:322:11)
#17 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
#18 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
#19 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
#20 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
#24 _invoke1 (dart:ui/hooks.dart:170:10)
#25 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331:7)
#26 _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#12cd2
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(239.2, 911.2)
finalLocalPosition: Offset(239.2, 11.2)
button: 1
sent tap down
DB HELPER FILE:
import 'package:sqflite/sqflite.dart' as sql;
import 'package:path/path.dart' as p;
class dbHelper{
static Future<sql.Database> DataBase() async{
return sql.openDatabase(p.join(await sql.getDatabasesPath(),'memory_place.db'),
onCreate: (db, version) async{
return await db.execute('CREATE TABLE placestore(id TEXT PRIMARY KEY, title TEXT, video TEXT, thumbnail TEXT)');
},version: 1);
}
static Future<void> insert(String Table,Map<String,Object> data) async{
final db = await dbHelper.DataBase();
db.insert(
Table,
data,
conflictAlgorithm: sql.ConflictAlgorithm.replace);
}
static Future<List<Map<String,dynamic>>> getData(String table) async{
final db = await dbHelper.DataBase();
return db.query(table);
}
}
IMAGE INPUT PAGE :
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart' as syspath;
import 'package:path/path.dart' as p;
import 'package:video_thumbnail/video_thumbnail.dart';
class ImageInput extends StatefulWidget {
Function onSelected;
ImageInput({required this.onSelected});
#override
State<ImageInput> createState() => _ImageInputState();
}
class _ImageInputState extends State<ImageInput> {
XFile ? _referenceXFileVid;
File? _referenceVidFile;
Future<void> TakePic() async{
final _imagePicker = ImagePicker();
final NewFile = await _imagePicker.pickVideo(
source: ImageSource.camera,
maxDuration: Duration(minutes: 1));
setState(() {
_referenceVidFile = File(NewFile!.path);
_referenceXFileVid = NewFile;
});
}
Future<Uint8List?> VideoThumbNail(XFile Video) async{
final uint8list = await VideoThumbnail.thumbnailData(
video: Video.path,
imageFormat: ImageFormat.JPEG,
maxWidth: 150,
quality: 25,
);
return await uint8list;
}
#override
Widget build(BuildContext context) {
return Row(
children: [
Container(
height: 267,
width: 150,
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border.all(width: 1,color: Colors.grey),
),
child: _referenceVidFile!= null ?
FutureBuilder<Uint8List?>(
future: VideoThumbNail(_referenceXFileVid!),
builder: (context,uint8) {
if(uint8.hasData){
final ImagePath = uint8.data;
final CurrentThumbImg = Image.memory(ImagePath!,
height: 267,
width: 150,
fit: BoxFit.cover,);
SaveVidAndThumb(_referenceVidFile!,File.fromRawPath(ImagePath));
return CurrentThumbImg;
}else{
return Center(child: Text('Saumya Made Mistake',textAlign: TextAlign.center,));
}
}) : Text('No Video Taken',textAlign: TextAlign.center,),
),
SizedBox(width: 10,),
Expanded(
child: FlatButton.icon(onPressed: (){
TakePic();
},
icon: Icon(Icons.camera),
label: Text('Take Video'),
textColor: Theme.of(context).primaryColor,),
),
],
);
}
void SaveVidAndThumb(File Video,File Thumbnail) async{
final appDir = await syspath.getApplicationDocumentsDirectory();
final VideoPath = p.basename(Video.path);
final ThumbPath = p.basename(Thumbnail.path);
final Savedvideo = await Video.copy('${appDir.path}/$VideoPath');
final SavedThumb = await Thumbnail.copy('${appDir.path}/$ThumbPath');
widget.onSelected(Savedvideo,SavedThumb);
}
}
ADD PLACES SCREEN :
import 'dart:io';
import 'package:memory_place/models/Video_format.dart';
import 'package:memory_place/providers/place_provider.dart';
import 'package:memory_place/widgets/Location_Input.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import 'package:memory_place/widgets/Image_Input.dart';
class AddScreen extends StatelessWidget {
TextEditingController _TitleTextController = TextEditingController();
late File _CurrentVideo;
late File _CurrentThumbnail;
void CallCurrentImage(File CurrentVideo,File CurrentThumbnail){
if(CurrentVideo!=null){
_CurrentVideo = CurrentVideo;
_CurrentThumbnail = CurrentThumbnail;
}
}
#override
Widget build(BuildContext context) {
void SafePlace(){
if(_TitleTextController.text == null || _CurrentThumbnail == null){
throw Text('Either Title or Image is Missing');
}else {
Provider.of<PlaceProvider>(context, listen: false).AddPlace(
_TitleTextController.text,
_CurrentVideo,
_CurrentThumbnail);
Navigator.pop(context);
}
}
return Scaffold(
appBar: AppBar(
title: Text('Add Place Screen'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child:SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: [
TextField(
decoration: InputDecoration(label: Text('Title...')),
controller: _TitleTextController,
),
SizedBox(height: 20,),
ImageInput(onSelected: CallCurrentImage),
SizedBox(height: 20,),
LocationInput(),
],
),
),
) ),
RaisedButton.icon(
onPressed: (){
SafePlace();
},
icon: Icon(Icons.add),
label: Text('Add Place'),
elevation: 0,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Theme.of(context).accentColor,
)
],
),
);
}
}

When you declare a variable as late , you are actually indicating that it won't be null.
In your code, you're declaring_CurrentThumbnail variable as late but not
instantiating it.
You can change your AddScreen widget to StatefulWidget and instantiate _CurrentThumbnail inside initState function.(or even call CallCurrentImage inside initState)

Related

Flutter/dart:: Adding a new field to class causing an error

I added a new field to the class CloudNote, now I am getting an error!
I am getting the error when the app is trying to display a list.
Here is all my code without adding the field :: https://github.com/casas1010/flutter_firebase_vendor_management
I know its a simple issue, but I have tried to troubleshoot this for like an hour and have not made any progress
CloudNote class::
import 'package:cloud_firestore/cloud_firestore.dart';
import '/services/cloud/cloud_storage_constants.dart';
import 'package:flutter/foundation.dart';
/*
https://youtu.be/VPvVD8t02U8?t=87934
*/
#immutable
class CloudNote {
final String documentId;
final String jobCreatorId;
final String jobDescription;
final String jobState; // I added this
const CloudNote({
required this.documentId,
required this.jobCreatorId,
required this.jobDescription,
required this.jobState, // I added this
});
// acts as constructor
CloudNote.fromSnapshot(QueryDocumentSnapshot<Map<String, dynamic>> snapshot)
: documentId = snapshot.id,
jobCreatorId = snapshot.data()[jobCreatorIdColumn],
jobState = snapshot.data()[jobStateColumn], // I added this
jobDescription = snapshot.data()[jobDescriptionColumn] as String;
}
notes view ::
import 'package:flutter/material.dart';
import '/constants/routes.dart';
import '/enums/menu_action.dart';
import '/services/auth/auth_service.dart';
import '/services/cloud/cloud_note.dart';
import '/services/cloud/firebase_cloud_storage.dart';
import '/utilities/dialogs/logout_dialog.dart';
import '/views/notes/notes_list_view.dart';
class NotesView extends StatefulWidget {
const NotesView({Key? key}) : super(key: key);
#override
_NotesViewState createState() => _NotesViewState();
}
class _NotesViewState extends State<NotesView> {
late final FirebaseCloudStorage _notesService;
String get userId => AuthService.firebase().currentUser!.id;
#override
void initState() {
_notesService = FirebaseCloudStorage();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Your jobs'),
actions: [
IconButton(
onPressed: () {
Navigator.of(context).pushNamed(createOrUpdateNoteRoute);
},
icon: const Icon(Icons.add),
),
PopupMenuButton<MenuAction>(
onSelected: (value) async {
switch (value) {
case MenuAction.logout:
final shouldLogout = await showLogOutDialog(context);
if (shouldLogout) {
await AuthService.firebase().logOut();
Navigator.of(context).pushNamedAndRemoveUntil(
loginRoute,
(_) => false,
);
}
}
},
itemBuilder: (context) {
return const [
PopupMenuItem<MenuAction>(
value: MenuAction.logout,
child: Text('Log out'),
),
];
},
)
],
),
body: StreamBuilder(
stream: _notesService.allNotes(jobCreatorId: userId),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
case ConnectionState.active:
if (snapshot.hasData) {
final allNotes = snapshot.data as Iterable<CloudNote>;
return NotesListView(
notes: allNotes,
onDeleteNote: (note) async {
await _notesService.deleteNote(documentId: note.documentId);
},
onTap: (note) {
Navigator.of(context).pushNamed(
createOrUpdateNoteRoute,
arguments: note,
);
},
);
} else {
return const CircularProgressIndicator();
}
default:
return const CircularProgressIndicator();
}
},
),
);
}
}
Error
The following _TypeError was thrown building NotesListView(dirty):
type 'Null' is not a subtype of type 'String'
The relevant error-causing widget was
NotesListView
lib/…/notes/notes_view.dart:72
When the exception was thrown, this was the stack
#0 new CloudNote.fromSnapshot
package:ijob_clone_app/…/cloud/cloud_note.dart:26
#1 FirebaseCloudStorage.allNotes.<anonymous closure>.<anonymous closure>
package:ijob_clone_app/…/cloud/firebase_cloud_storage.dart:39
#2 MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
#3 ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
#4 WhereIterator.moveNext (dart:_internal/iterable.dart:438:22)
#5 Iterable.length (dart:core/iterable.dart:497:15)
#6 NotesListView.build
package:ijob_clone_app/…/notes/notes_list_view.dart:26
#7 StatelessElement.build
package:flutter/…/widgets/framework.dart:4949
#8 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4878
#9 Element.rebuild
package:flutter/…/widgets/framework.dart:4604
#10 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4859
#11 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4853
#12 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3863
#13 Element.updateChild
package:flutter/…/widgets/framework.dart:3586
#14 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4904
#15 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:5050
#16 Element.rebuild
package:flutter/…/widgets/framework.dart:4604
#17 BuildOwner.buildScope
package:flutter/…/widgets/framework.dart:2667
#18 WidgetsBinding.drawFrame
package:flutter/…/widgets/binding.dart:882
#19 RendererBinding._handlePersistentFrameCallback
package:flutter/…/rendering/binding.dart:378
#20 SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1175
#21 SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1104
#22 SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:1015
#23 _invoke (dart:ui/hooks.dart:148:13)
#24 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:318:5)
#25 _drawFrame (dart:ui/hooks.dart:115:31)
════════════════════════════════════════════════════════════════════════════════
NotesListView ::
import 'package:flutter/material.dart';
import '/services/cloud/cloud_note.dart';
import '/utilities/dialogs/delete_dialog.dart';
/*
source: https://www.youtube.com/watch?v=VPvVD8t02U8&t=59608s
class creation :: 22:02:54
*/
typedef NoteCallback = void Function(CloudNote note);
class NotesListView extends StatelessWidget {
final Iterable<CloudNote> notes; // list of notes
final NoteCallback onDeleteNote;
final NoteCallback onTap;
const NotesListView({
Key? key,
required this.notes,
required this.onDeleteNote,
required this.onTap,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: notes.length,
itemBuilder: (context, index) {
final note =
notes.elementAt(index); // current note whose data we are returning
return ListTile(
onTap: () {
onTap(note);
},
title: Text(
note.jobDescription,
maxLines: 1,
softWrap: true,
overflow: TextOverflow.ellipsis,
),
trailing: IconButton(
onPressed: () async {
final shouldDelete = await showDeleteDialog(context);
if (shouldDelete) {
onDeleteNote(note);
}
},
icon: const Icon(Icons.delete),
),
);
},
);
}
}
This happens if you forget to update your Firestore entries. Atleast one of your CloudNote entries in Firestore does not have the field jobState. That's why Firestore returns a Null value. But it tries to map to String which leads to an exception.
Make sure to rerun the project.
or
flutter clean
and then
flutter run

Flutter Error: StackOverflowError was thrown building Consumer<LocationProvider>(dirty, dependencies: [_InheritedProviderScope<LocationProvider?>])

i'm trying to include live location tracking into my app using the following packages:
google_maps_flutter 2.1.2 --> https://pub.dev/packages/google_maps_flutter/install
location 4.3.0 --> https://pub.dev/packages/location
provider 6.0.2 --> https://pub.dev/packages/provider
I am receiving the following error:
════════ Exception caught by widgets library ═══════════════════════════════════
The following StackOverflowError was thrown building Consumer<LocationProvider>(dirty, dependencies: [_InheritedProviderScope<LocationProvider?>]):
Stack Overflow
The relevant error-causing widget was
Consumer<LocationProvider>
lib/google_map_page.dart:31
When the exception was thrown, this was the stack
#0 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#1 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#2 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#3 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#4 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#5 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#6 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#7 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#8 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#9 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#10 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#11 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#12 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#13 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#14 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#15 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#16 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#17 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#18 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#19 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#20 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#21 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#22 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#23 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#24 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#25 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
#26 LocationProvider.locationPosition
package:map_test/provider/location_provider.dart:55
......
package:map_test/provider/location_provider.dart:55 -->
LatLng get locationPosition => locationPosition;
There is 3 classes:
main.dart
google_map_page.dart
location_provider.dart
1. main.dart
import 'package:flutter/material.dart';
import 'package:map_test/google_map_page.dart';
import 'package:map_test/provider/location_provider.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => LocationProvider(),
child: const GoogleMapPage(),
)
],
child: MaterialApp(
title: 'Flutter Demo',
home: const GoogleMapPage(),
),
);
}
}
2. google_map_page.dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:map_test/provider/location_provider.dart';
import 'package:provider/provider.dart';
class GoogleMapPage extends StatefulWidget {
const GoogleMapPage({Key? key}) : super(key: key);
#override
State<GoogleMapPage> createState() => _GoogleMapPageState();
}
class _GoogleMapPageState extends State<GoogleMapPage> {
#override
void initState() {
super.initState();
Provider.of<LocationProvider>(context, listen: false).initalization();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Map2'),
backgroundColor: Colors.red,
),
body: googleMapUI());
}
Widget googleMapUI() {
return Consumer<LocationProvider>(builder: (consumerContext, model, child) {
if (model.locationPosition != null) {
return Column(
children: [
Expanded(
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition:
CameraPosition(target: model.locationPosition, zoom: 18),
myLocationEnabled: true,
myLocationButtonEnabled: true,
onMapCreated: (GoogleMapController controller) {},
),
)
],
);
}
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
});
}
}
3.location_provider.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
class LocationProvider with ChangeNotifier {
// Widget loc() {
// throw '';
// }
bool locationServiceActive = true;
LocationProvider() {
Location location;
location = new Location();
}
Location get location => location;
initalization() async {
await getUserLocation();
}
getUserLocation() async {
bool _serviceEnabled;
PermissionStatus _permissionGranted;
LatLng locationPosition;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
location.onLocationChanged.listen((LocationData currentLocation) {
locationPosition = LatLng(
currentLocation.latitude!,
currentLocation.longitude!,
);
print(locationPosition);
notifyListeners();
});
}
LatLng get locationPosition => locationPosition; // line 55
}
It's my first time coding in flutter, i faced a lot of problem because of the Null Safety already. it just seems like the problems are endless.
any recommendations will be much appreciated.
Thank you.
The StackOverflowError is caused by self reference
LatLng get locationPosition => locationPosition;
means locationPosition is itself.
Hence will suggest you to amend the internal parameter variable by having prefix _
LatLng get locationPosition => _locationPosition;
while your class params and constructor can be written like below
class LocationProvider with ChangeNotifier {
LatLng? _locationPosition;
final Location _location = new Location();
LocationProvider();
Location get location => _location;
LatLng? get locationPosition => _locationPosition;
// other parts
}
I am not sure but try to wrap consumer in build method
Like this:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Map2'),
backgroundColor: Colors.red,
),
body: Consumer<LocationProvider>(builder: (consumerContext, model, child) {
return googleMapUI()});
}
And add needed parameters

Error with IBM Visual Recognition Classification with Flutter

I am trying to take an image and send it to IBM Watson to classify it into one of 3 custom classifiers. Below is all my code.
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ibm_watson/flutter_ibm_watson.dart';
import 'package:ibm_visual_recog_img_file/connection.dart';
import 'package:ourearth2020/screens/Community.dart';
import 'package:path/path.dart';
import 'dart:async';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:path_provider/path_provider.dart';
class VisualPage extends StatefulWidget {
#override
_VisualPageState createState() => _VisualPageState();
}
class _VisualPageState extends State<VisualPage> {
CameraController _controller;
List cameras;
String path;
var galleryImage;
CameraDescription cameraDescription;
Future initCamera() async {
cameras = await availableCameras();
var frontCamera = cameras.first;
_controller = CameraController(frontCamera, ResolutionPreset.high);
try {
await _controller.initialize();
} catch (e) {}
print('Controller Is Init:' + _controller.value.isInitialized.toString());
displayPreview();
}
bool displayPreview() {
if (_controller == null || !_controller.value.isInitialized) {
return false;
} else {
return true;
}
}
Future getImageFromGallery() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
galleryImage = image;
});
print('GALLERY IMAGE' + galleryImage.toString());
return galleryImage;
}
#override
void dispose() {
// Dispose of the controller when the widget is disposed.
_controller.dispose();
super.dispose();
}
#override
void initState() {
super.initState();
print('Running');
initCamera();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Stack(children: [
displayPreview()
? AspectRatio(
aspectRatio: MediaQuery.of(context).size.width /
MediaQuery.of(context).size.height,
child: CameraPreview(_controller),
)
: Container(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.yellow),
),
),
Positioned(
top: MediaQuery.of(context).size.height - 120,
child: GestureDetector(
onTap: () async {
await getImageFromGallery();
Navigator.push(context, MaterialPageRoute(builder: (context) =>
DisplayPicture(image: galleryImage)
));
},
child: Icon(
Icons.image,
color: Colors.white,
size: 60,
)),
),
Positioned(
top: MediaQuery.of(context).size.height - 120,
left: MediaQuery.of(context).size.width / 2.2,
child: GestureDetector(
behavior: HitTestBehavior.translucent,
child: Container(
child: Icon(
Icons.camera,
color: Colors.white,
size: 60,
)),
onTap: () async {
final path = (await getTemporaryDirectory()).path +
'${DateTime.now()}.png';
try {
await _controller.takePicture(path);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DisplayPicture(imagePath: path)));
} catch (e) {
print('EEEE' + e);
}
}))
]));
}
}
class DisplayPicture extends StatelessWidget {
String imagePath;
File image;
String _text;
// File file = File(imagePath)
DisplayPicture({this.imagePath, this.image});
visualImageClassifier(File image) async{
IamOptions options = await IamOptions(iamApiKey: "NRDjngCby2d-pSHOPyWQJxhuB6vOY2uOTCX6KV2BCfwB", url: "https://api.us-south.visual-recognition.watson.cloud.ibm.com/instances/ef286f4e-84c7-44e0-b63d-a6a49a142a30").build();
VisualRecognition visualRecognition = new VisualRecognition(iamOptions: options, language: Language.ENGLISH); // Language.ENGLISH is language response
ClassifiedImages classifiedImages = await visualRecognition.classifyImageFile(image.path);
print(classifiedImages.getImages()[0].getClassifiers()[0]
.getClasses()[0]
.className);
// print("${image.toString()}");
// print('ACCESS'+options.accessToken);
//print(options);
//print("${image.path}");
//print('CLASSIFICATION'+classifiedImages.customClasses.toString()); // StreamBuilder(
// stream: StreamMyClassifier(
// image,
// 'NRDjngCby2d-pSHOPyWQJxhuB6vOY2uOTCX6KV2BCfwB', 'CompostxLandfillxRecycle_2056123069'),
// builder: (context, snapshot) {
// if (snapshot.hasData) {
// _text = snapshot.data;
// print(_text);
// }
// else {
// print('NO DATA AVAILABLE');
// }
//
// }
// );
}
#override
Widget build(BuildContext context) {
return Scaffold(body:Stack(children:[Center(child:image==null?Image.file(File(imagePath)):Image.file(image)),Positioned(
top: MediaQuery.of(context).size.height/2,
child: FloatingActionButton(onPressed:() async{
await visualImageClassifier(image==null?File(imagePath):image);
},
child:Icon(Icons.arrow_right)),
)]));
}
}
The image is successfully displayed on my screen but once I send it through the visualRecognition.classifyImageFile(....); it gives me an error saying I can not use image because it only supports String. I converted it to String but it gives me the error below.
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: FileSystemException: Cannot retrieve length of file, path = 'File: '/data/user/0/com.example.ourearth2020/cache2020-09-17 18:50:16.530957.png'' (OS Error: No such file or directory, errno = 2)
E/flutter (17606): #0 _File.length.<anonymous closure> (dart:io/file_impl.dart:366:9)
E/flutter (17606): #1 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (17606): #2 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (17606): #3 _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
E/flutter (17606): #4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
E/flutter (17606): #5 Future._propagateToListeners (dart:async/future_impl.dart:725:32)
E/flutter (17606): #6 Future._completeWithValue (dart:async/future_impl.dart:529:5)
E/flutter (17606): #7 Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:567:7)
E/flutter (17606): #8 _rootRun (dart:async/zone.dart:1190:13)
E/flutter (17606): #9 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (17606): #10 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (17606): #11 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (17606): #12 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (17606): #13 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (17606):
Some of the questions I have: Can I get the confidence score using this? The last time I tried using those statements to classify the image it used a general classifier(if I input an image of a skyscraper it told me skyscraper) so how can I have it classify with my 3 classifiers?
Btw I already set up the IBM Cloud and it is fully functional. The library that I found from pub.dev is here https://pub.dev/packages/flutter_ibm_watson
EDIT error code for getImages() method
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'getImages' was called on null.
E/flutter (31403): Receiver: null
E/flutter (31403): Tried calling: getImages()
E/flutter (31403): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
E/flutter (31403): #1 DisplayPicture.visualImageClassifier (package:ourearth2020/screens/VisualPage.dart:147:30)
E/flutter (31403): <asynchronous suspension>
E/flutter (31403): #2 DisplayPicture.build.<anonymous closure> (package:ourearth2020/screens/VisualPage.dart:177:15)
E/flutter (31403): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
E/flutter (31403): #4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
E/flutter (31403): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
E/flutter (31403): #6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
E/flutter (31403): #7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
E/flutter (31403): #8 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:219:7)
E/flutter (31403): #9 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:477:9)
E/flutter (31403): #10 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:78:12)
E/flutter (31403): #11 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:124:9)
E/flutter (31403): #12 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter (31403): #13 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:122:18)
E/flutter (31403): #14 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:108:7)
E/flutter (31403): #15 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:220:19)
E/flutter (31403): #16 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
E/flutter (31403): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
E/flutter (31403): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
E/flutter (31403): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
E/flutter (31403): #20 _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (31403): #21 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (31403): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (31403): #23 _invoke1 (dart:ui/hooks.dart:283:10)
E/flutter (31403): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:192:5)
E/flutter (31403):
You can copy past run full code below
You can see confidence score in working demo below
please change image.toString() to image.path because image is File
from
ClassifiedImages classifiedImages = await visualRecognition.classifyImageFile(image.toString());
to
ClassifiedImages classifiedImages = await visualRecognition.classifyImageFile(image.path);
working demo
working demo 2 for CameraPreview
I/flutter (31132): living room
full code
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter_ibm_watson/flutter_ibm_watson.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
class VisualPage extends StatefulWidget {
#override
_VisualPageState createState() => _VisualPageState();
}
class _VisualPageState extends State<VisualPage> {
CameraController _controller;
List cameras;
String path;
var galleryImage;
CameraDescription cameraDescription;
Future initCamera() async {
cameras = await availableCameras();
var frontCamera = cameras.first;
_controller = CameraController(frontCamera, ResolutionPreset.high);
try {
await _controller.initialize();
} catch (e) {}
print('Controller Is Init:' + _controller.value.isInitialized.toString());
displayPreview();
setState(() {});
}
bool displayPreview() {
if (_controller == null || !_controller.value.isInitialized) {
return false;
} else {
return true;
}
}
Future getImageFromGallery() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
galleryImage = image;
});
print('GALLERY IMAGE' + galleryImage.toString());
return galleryImage;
}
#override
void dispose() {
// Dispose of the controller when the widget is disposed.
_controller.dispose();
super.dispose();
}
#override
void initState() {
super.initState();
print('Running');
initCamera();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Stack(children: [
displayPreview()
? AspectRatio(
aspectRatio: MediaQuery.of(context).size.width /
MediaQuery.of(context).size.height,
child: CameraPreview(_controller),
)
: Container(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.yellow),
),
),
Positioned(
top: MediaQuery.of(context).size.height - 120,
child: GestureDetector(
onTap: () async {
await getImageFromGallery();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DisplayPicture(image: galleryImage)));
},
child: Icon(
Icons.image,
color: Colors.white,
size: 60,
)),
),
Positioned(
top: MediaQuery.of(context).size.height - 120,
left: MediaQuery.of(context).size.width / 2.2,
child: GestureDetector(
behavior: HitTestBehavior.translucent,
child: Container(
child: Icon(
Icons.camera,
color: Colors.white,
size: 60,
)),
onTap: () async {
final path = (await getTemporaryDirectory()).path +
'${DateTime.now()}.png';
try {
await _controller.takePicture(path);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DisplayPicture(imagePath: path)));
} catch (e) {
print('EEEE' + e);
}
}))
]));
}
}
class DisplayPicture extends StatelessWidget {
String imagePath;
File image;
String _text;
// File file = File(imagePath)
DisplayPicture({this.imagePath, this.image});
visualImageClassifier(File image) async {
IamOptions options = await IamOptions(
iamApiKey: "NRDjngCby2d-pSHOPyWQJxhuB6vOY2uOTCX6KV2BCfwB",
url:
"https://api.us-south.visual-recognition.watson.cloud.ibm.com/instances/ef286f4e-84c7-44e0-b63d-a6a49a142a30")
.build();
VisualRecognition visualRecognition = new VisualRecognition(
iamOptions: options,
language: Language.ENGLISH); // Language.ENGLISH is language response
ClassifiedImages classifiedImages =
await visualRecognition.classifyImageFile(image.path);
print(classifiedImages
.getImages()[0]
.getClassifiers()[0]
.getClasses()[0]
.className);
// print("${image.toString()}");
// print('ACCESS'+options.accessToken);
//print(options);
//print("${image.path}");
//print('CLASSIFICATION'+classifiedImages.customClasses.toString()); // StreamBuilder(
// stream: StreamMyClassifier(
// image,
// 'NRDjngCby2d-pSHOPyWQJxhuB6vOY2uOTCX6KV2BCfwB', 'CompostxLandfillxRecycle_2056123069'),
// builder: (context, snapshot) {
// if (snapshot.hasData) {
// _text = snapshot.data;
// print(_text);
// }
// else {
// print('NO DATA AVAILABLE');
// }
//
// }
// );
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(children: [
Center(
child:
image == null ? Image.file(File(imagePath)) : Image.file(image)),
Positioned(
top: MediaQuery.of(context).size.height / 2,
child: FloatingActionButton(
onPressed: () async {
await visualImageClassifier(
image == null ? File(imagePath) : image);
},
child: Icon(Icons.arrow_right)),
)
]));
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: VisualPage(),
);
}
}

How to read file txt from storage (Flutter)?

I want to read a config file from a folder on my phone. But I manage to read a part of file and I want to be able to read the entire file. Any Ideas about how to do it the right way?
Here is my code :
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:ext_storage/ext_storage.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// String _platformVersion = 'Unknown';
String _configFile = 'No Data';
static Future<String> get _getPath async {
final localPath = await ExtStorage.getExternalStoragePublicDirectory(
ExtStorage.DIRECTORY_DOWNLOADS);
return localPath;
}
static Future<File> get _localFile async {
final path = await _getPath;
return File('$path/config_test.txt');
}
static Future<String> readConfig() async {
try {
final file = await _localFile;
// Read the file.
String contents = await file.readAsString();
return contents;
} catch (e) {
// If encountering an error, return 0.
return "error";
}
}
#override
void initState() {
super.initState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
if (await FlutterOpenvpn.init(
localizedDescription: "ExampleVPN",
providerBundleIdentifier:
"com.topfreelancerdeveloper.flutterOpenvpnExample.RunnerExtension",
)) {
await FlutterOpenvpn.lunchVpn(
_configFile,
(isProfileLoaded) => print('isProfileLoaded : $isProfileLoaded'),
(vpnActivated) => print('vpnActivated : $vpnActivated'),
//expireAt: DateTime.now().add(Duration(seconds: 30)),
);
}
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('OpenVPN connect Test'),
),
body: Center(
child: Column(
children: <Widget>[
IconButton(
iconSize: 48.0,
splashColor: Colors.greenAccent,
onPressed: _startBtnVpn,
icon: Icon(Icons.play_arrow)),
IconButton(
iconSize: 48.0,
splashColor: Colors.greenAccent,
icon: Icon(Icons.stop),
onPressed: _stopBtnVPN,
),
IconButton(
iconSize: 48.0,
splashColor: Colors.greenAccent,
icon: Icon(Icons.cloud),
onPressed: () {
readConfig().then((value) {
setState(() {
_configFile = value;
print(_configFile);
});
});
}),
],
),
),
),
);
}
void _startBtnVpn() {
setState(() {
initPlatformState();
});
}
void _stopBtnVPN() {
FlutterOpenvpn.stopVPN();
}
}
DEBUG CONSOLE:
I/flutter (17341): "dev tun\n" +
I/flutter (17341): "proto udp\n" +
I/flutter (17341): "remote public-vpn-255.opengw.net 1195\n" +
I/flutter (17341): ";http-proxy-retry\n" +
I/flutter (17341): ";http-proxy [proxy server] [proxy port]\n" +
I/flutter (17341): "cipher AES-128-CBC\n" +
I/flutter (17341): "auth SHA1\n" +
I/flutter (17341): "resolv-retry infinite\n" +
I/flutter (17341): "nobind\n" +
I/flutter (17341): "persist-key\n" +
I/flutter (17341): "persist-tun\n" +
I/flutter (17341): "client\n" +
I/flutter (17341): "verb 3\n" +
I/flutter (17341): "<ca>\n" +
I/flutter (17341): "-----BEGIN CERTIFICATE-----\n" +
I/flutter (17341): "MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB\n" +
I/flutter (17341): "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" +
I/flutter (17341): "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" +
I/flutter (17341): "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw\n" +
I/flutter (17341): "MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV\n" +
I/flutter (17341): "BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU\n" +
I/flutter (17341): "aGUgVVNFUlR
The problem is that the print function has a limitation for printing outputs to the console, and it doesn't print long strings completely. Try setting a break point using your IDE or editor and check out the value read from the file.

Singleton in Flutter gives runtime error "Unhandled Exception: Reading static variable '_instance#545324594' during its initialization"

I tried to implement the MVP-Pattern in a Flutter App (Flutter version 1.12.13+hotfix8).
My controller (presenter) is a singleton and looks like this:
import 'package:flutter/material.dart';
// local file imports
import 'package:prototype/gui/screens/welcome/welcome.dart';
// this is a singleton
class Controller {
static final Controller _instance = Controller._internal();
Widget _currentWidget;
Controller._internal() {
this._currentWidget = ScreenWelcome();
}
factory Controller() => _instance;
Widget get currentWidget => this._currentWidget;
set currentWidget(Widget widget){
_currentWidget = widget;
}
}
My home screen looks like:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
// local file imports
import 'package:prototype/controller/conroller.dart';
import 'package:prototype/gui/screens/register/register.dart';
import 'package:prototype/gui/screens/register/sign_in.dart';
import 'package:prototype/gui/screens/text_viewer/text_viewer.dart';
class ScreenWelcome extends StatelessWidget {
final _controller = Controller();
#override
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Image.asset('resources/zombiepaar.jpg',
width: 500, height: 500, fit: BoxFit.fitWidth),
SizedBox(
height: 20.0,
),
RaisedButton(
child: Text("Ein neues Konto erstellen"),
onPressed: () => _controller.currentWidget = ScreenRegister(),
),
SizedBox(
height: 20.0,
),
RaisedButton(
child: Text("Mit bestehendem Konto einloggen"),
onPressed: () => _controller.currentWidget = ScreenSignIn(),
),
SizedBox(
height: 20.0,
),
Row(
children: <Widget>[
GestureDetector(
child: Text(
"Nutzungsbedingungen",
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.blue),
),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ScreenTextViewer(
title: "Nutzungsbedingungen",
ressourceFileToLoad: 'resources/AGBs.txt'),
)),
),
SizedBox(
width: 20.0,
),
GestureDetector(
child: Text(
"Datenschutzrichtlinien",
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.blue),
),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ScreenTextViewer(
title: "Nutzungsbedingungen",
ressourceFileToLoad: 'resources/AGBs.txt'),
),
),
),
],
),
],
),
),
);
}
}
But when I ran my app I get the following runtime error:
D/FlutterActivityAndFragmentDelegate(21372): Executing Dart entrypoint: main, and sending initial route: /
E/flutter (21372): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Reading static variable '_instance#545324594' during its initialization
E/flutter (21372): #0 Controller._instance (package:prototype/controller/conroller.dart:9:27)
E/flutter (21372): #1 new Controller (package:prototype/controller/conroller.dart:17:27)
E/flutter (21372): #2 new ScreenWelcome (package:prototype/gui/screens/welcome/welcome.dart:11:23)
E/flutter (21372): #3 new Controller._internal (package:prototype/controller/conroller.dart:14:27)
E/flutter (21372): #4 Controller._instance (package:prototype/controller/conroller.dart:9:50)
E/flutter (21372): #5 Controller._instance (package:prototype/controller/conroller.dart:9:27)
E/flutter (21372): #6 new Controller (package:prototype/controller/conroller.dart:17:27)
E/flutter (21372): #7 new MyApp (package:prototype/main.dart:10:22)
E/flutter (21372): #8 main (package:prototype/main.dart:6:23)
E/flutter (21372): #9 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
E/flutter (21372): #10 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (21372): #11 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (21372): #12 _runZoned (dart:async/zone.dart:1518:10)
E/flutter (21372): #13 runZoned (dart:async/zone.dart:1502:12)
E/flutter (21372): #14 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter (21372): #15 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter (21372): #16 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
Any idea how to fix this? I found nothing helpful with Google and I'm pretty new to dart and Flutter. Maybe I implemented the singleton class the wrong way?
Edit: Mi main.dart file is
import 'package:flutter/material.dart';
// local file imports
import 'package:prototype/controller/conroller.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of the application.
final controller = Controller(); // presenter in MVP design pattern
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
home: controller.currentWidget, // controller decides the main widget
);
}
}
You have a cyclic dependency. The creation of a ScreenWelcome calls the Controller constructor, which reads the _instance field, which constructs a Controller, which creats a ScreenWelcome. You're lucky that the _instance field is lazy because then it detects the cycle early instead of hitting a stack overflow.
If you have two classes with final fields which must point to each other. That's basically impossible. Luckily the _currentWidget is not final, so that's the one you need to set after both objects have been created.
I'd do something like:
class Controller {
static final _instance = Controller._();
Widget_currentWidget;
factory Controller() => _instance;
Controller._();
}
class ScreenWelcome {
final Controller _controller;
ScreenWelcome() : _controller = Controller() {
// This is the soonest a reference to this widget is available.
_controller.currentWidget = this;
}
}
Finally, I found the answer: I have to call the constructor of the widget as late as possible. Therefore, I called it in the getter function. The working code looks like:
import 'package:flutter/material.dart';
// local file imports
import 'package:prototype/gui/screens/welcome/welcome.dart';
// this is a singleton
class Controller {
static final Controller _instance = Controller._internal();
Function _currentWidget;
Controller._internal() {
this._currentWidget = () => ScreenWelcome();
}
factory Controller() => _instance;
Widget get currentWidget => this._currentWidget();
set currentWidget(Widget widget){
_currentWidget = () => widget;
}
}
Maybe one day it will help someone :)