Google Maps Place Picker package Error in Flutter - flutter

I am getting a huge list of error when I try to run the following code with google_maps_place_picker package in flutter.
import 'package:flutter/material.dart';
// import 'package:google_maps/google_maps.dart';
import 'package:google_maps/google_maps.dart';
import 'package:google_maps_place_picker/google_maps_place_picker.dart'
as place;
// import 'package:location/location.dart';
import '../components/location_helper.dart';
// import '../components/location_helper.dart';
class MapScreen extends StatelessWidget {
final LatLng location1 = LatLng(37.657, -122.776);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Map Screen'),
),
body: Center(
child: place.PlacePicker(
apiKey: GOOGLE_API,
useCurrentLocation: true,
onPlacePicked: (result) {
print(result.addressComponents);
Navigator.of(context).pop();
}),
));
}
}
Error
5:8: Error: Not found: 'dart:html' import 'dart:html' show Document,
Element, Node;
Error: Not found: 'dart:js' export 'dart:js' show allowInterop,
allowInteropCaptureThis;
Error: Not found: 'dart:js_util' export 'dart:js_util';
Error: Type 'Element' not found.
Element mapDiv, [
^^^^^^^
Error: Type 'Node' not found. List<MVCArray> get controls =>
^^^^
Error: Type 'Element' not found. Element _getDiv() =>
callMethod(this, 'getDiv', []); ^^^^^^^
These are just some of the errors I have put. There are MANY more like these.
I have added these dependencies in my pubspec.yaml file.
google_maps_flutter: ^1.2.0
geodesy: ^0.3.2
confirm_dialog: ^0.1.1
geocoding: ^1.0.5
geocoder: ^0.2.1
google_maps_place_picker: ^1.0.1
tuple: ^1.0.3
js: ^0.6.2
html: ^0.14.0+4

You are using import 'package:google_maps/google_maps.dart';
google_maps package only supports web. For mobile support should try out flutter_google_places, google_maps_flutter or any other package.

Related

GetIt package - Object/factory not registered inside GetIt

I am using the GetIt package in my Flutter project to manage dependencies. However, I'm facing an issue where the package is throwing an _AssertionError with the following message:
'package:get_it/get_it_impl.dart': Failed assertion: line 372 pos 7:
'instanceFactory != null': Object/factory with type Client is not
registered inside GetIt. (Did you accidentally do GetIt
sl=GetIt.instance(); instead of GetIt sl=GetIt.instance; Did you
forget to register it?)
I have tried to await the initialization of the dependencies inside the main function before running the app, but the error persists. I have no clue how to debug this issue.
Can anyone please guide me on how to resolve this error and properly register my dependencies with GetIt? Any help would be appreciated. Thank you.
dependencies:
bloc: ^8.1.1
flutter_bloc: ^8.1.2
get_it: ^7.2.0
code:
//main.dart
import 'package:bloc_app/theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'home_feature/home_screen.dart';
import 'home_feature/application/bloc/api_request_bloc.dart';
import 'package:bloc_app/injection.dart' as di;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await di.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: AppTheme.ligthTheme,
darkTheme: AppTheme.darkTheme,
themeMode: ThemeMode.system,
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: BlocProvider(
create: (context) => di.sl<ApiRequestBloc>(),
child: const MyHomePage(title: 'Bloc App'),
),
);
}
}
The Get_it instance is created globaly here.
//injection.dart
import 'package:bloc_app/home_feature/application/bloc/api_request_bloc.dart';
import 'package:bloc_app/home_feature/domain/repositories/advicer_repository.dart';
import 'package:bloc_app/home_feature/domain/usecases/advicer_usecases.dart';
import 'package:bloc_app/home_feature/infrastructure/datasources/advicer_remote_datasource.dart';
import 'package:bloc_app/home_feature/infrastructure/repositories/advicer_repository_impl.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart' as http;
final sl = GetIt.instance;
Future<void> init() async {
// Blocs
sl.registerFactory(() => ApiRequestBloc(usecases: sl()));
//Usecases
sl.registerLazySingleton(() => AdvicerUseCases(advicerRepository: sl()));
//Repositories
sl.registerLazySingleton<AdvicerRepository>(
() => AdvicerRepositoryImpl(advicerRemoteDataSource: sl()));
//Datasources
sl.registerLazySingleton<AdvicerRemoteDataSource>(
() => AdvicerRemoteDataSourceImpl(client: sl()));
//Extern
sl.registerLazySingleton(() => http.Client);
}
The used bloc that get used inside the main.dart
//api_request_bloc.dart
import 'package:bloc/bloc.dart';
import 'package:bloc_app/home_feature/domain/entities/advice_entity.dart';
import 'package:bloc_app/home_feature/domain/failures/failures.dart';
import 'package:bloc_app/home_feature/domain/usecases/advicer_usecases.dart';
import 'package:dartz/dartz.dart';
// ignore: depend_on_referenced_packages
import 'package:meta/meta.dart';
part './api_request_event.dart';
part './api_request_state.dart';
class ApiRequestBloc extends Bloc<ApiRequestEvent, ApiRequestState> {
final AdvicerUseCases usecases;
ApiRequestBloc({required this.usecases}) : super(ApiRequestInitial()) {
on<ApiRequestEvent>((event, emit) async {
emit(ApiRequestLoading());
Either<AdviceEntity, Failure> adviceOrFailure =
await usecases.getAdviceUsecase();
//If usecase gives error than state retunres falure otherwise the advice get shown
adviceOrFailure.fold(
(advice) => emit(ApiRequestLoaded(advice: advice.advice)),
(failure) => emit(ApiRequestError(
error: _mapFailureToError(failure),
)),
);
});
}
String _mapFailureToError(Failure failure) {
switch (failure.runtimeType) {
case ServerFailure:
return 'Error: ${failure.runtimeType} ~ could not communicate with the server.';
case GeneralFailure:
return 'Error: ${failure.runtimeType} ~ Could not define error.';
default:
return 'Error: ${failure.runtimeType} ~ Could not define error.';
}
}
}
You are missing () while registering http.Client in file injection.dart
//Extern
sl.registerLazySingleton(() => http.Client());

Flutter says ImageElement isn't defined

Tried to run the application using flutter run -d linux command.
I have imported 'dart:html' and have it in pubspec.yaml, also did pub clean and pub get:
dependencies:
flutter:
sdk: flutter
http: ^0.13.5
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
html: ^0.15.1
flutter_image: ^4.1.4
webview_flutter: ^3.0.4
widgets: ^1.4.5
But flutter gives an error:
ERROR: lib/main.dart:21:12: Error: The method 'ImageElement' isn't defined for the class 'MyApp'.
ERROR: - 'MyApp' is from 'package:test/main.dart' ('lib/main.dart').
ERROR: Try correcting the name to the name of an existing method, or defining a method named 'ImageElement'.
ERROR: (int _) => ImageElement()..src = imageUrl,
This is the peace of code with ImageElement:
import 'dart:html';
import 'package:flutter/material.dart';
//import 'dart:ui' as ui;
import 'dart:ui' as ui;
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
String imageUrl =
"https://www.nasa.gov/centers/jpl/images/content/756230main_pia08329-full.jpg";
ui.platformViewRegistry.registerViewFactory(
imageUrl,
(int _) => ImageElement()..src = imageUrl,
);
return HtmlElementView(viewType: imageUrl);
}
}

Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime - Using Hive Flutter with 2 Hive Boxes

Working on app that uses 2 Hive boxes in Flutter. My first time trying and after I made many changes I began getting error messages on launch:
[VERBOSE-2:shell.cc(89)] Dart Error: error: import of dart:mirrors is not supported in the current Dart runtime
[VERBOSE-2:dart_isolate.cc(144)] Could not prepare isolate.
[VERBOSE-2:runtime_controller.cc(389)] Could not create root isolate.
[VERBOSE-2:shell.cc(605)] Could not launch engine with configuration.
Error happens when launching main
import 'package:app/main_thing/main_thing_model.dart';
import 'package:app/respect_time/task.dart';
import 'package:flutter/material.dart';
import 'package:app/welcome_screen.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hive_flutter/hive_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
await Hive.initFlutter();
Hive.registerAdapter(MainThingModelAdapter());
await Hive.openBox<MainThingModel>('main_thing');
Hive.registerAdapter(TaskAdapter());
await Hive.openBox<Task>('to_do_box');
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 MaterialApp(
home: WelcomeScreen(),
);
}
}
Here are my dependancies, which seem to be causing this error for others:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
google_fonts: ^3.0.1
flutter_local_notifications: ^9.7.0
intl: ^0.17.0
timezone: ^0.8.0
flutter_native_timezone: ^2.0.0
hive_flutter: ^1.1.0
hive: ^2.2.3
equatable: ^2.0.3
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
hive_generator: ^1.1.3
build_runner: ^2.2.0
Any help identifying issue is greatly appreciated.

Flutter Hive package initialisation error

I tried use Hive package in my application. But when I initialised in my app get error message:
The following HiveError was thrown building MyApp(dirty): Box not
found. Did you forget to call Hive.openBox()?
Her is my code:
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart' as path_provider;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocDir = await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocDir.path);
runApp(MyApp());
final box = await Hive.openBox('storage');
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final box = Hive.box('storage');
return MaterialApp(
title: 'Test App',
debugShowCheckedModeBanner: false,
home: CheckAuth(),
);
}
}
class CheckAuth extends StatefulWidget {
#override
_CheckAuthState createState() => _CheckAuthState();
}
class _CheckAuthState extends State<CheckAuth> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('Hive initialised!'),
);
}
}
Emulator
API: 28
Android: 9
Packages
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
hive: ^1.4.4+1
hive_flutter: ^0.3.1
http: ^0.12.2
cupertino_icons: ^1.0.0
path_provider: ^1.6.24
dev_dependencies:
flutter_test:
sdk: flutter
hive_generator: ^0.8.2
build_runner: ^1.10.11
flutter:
uses-material-design: true
Where I have any error in my code?
Why don't you use await Hive.initFlutter() instead of Hive.init()? The former is supposed to take care of proper app folder path on specific platform and put it's files there.
It is part of package:hive_flutter/hive_flutter.dart which deals with Flutter specific stuff

Unable to build Flutter Web and Flutter Mobile apps seperately

I am building a flutter project and am having an issue integrating the web and mobile code in a single project. I want to use Moor and Moor_FFI in my mobile code, but even though the entry point to my web (main.dart) and mobile code (main.dev.dart) are configured to be different to debug, it still tries to compile the mobile code for the web. This causes an issue, because FFI and other Dart plugins are not supported on Flutter Web as of now, resulting in a massive error message.
Error compiling dartdevc module:ffi|lib/ffi.ddc.js
packages/ffi/src/utf8.dart:6:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';
^
packages/ffi/src/utf16.dart:6:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';
^
packages/ffi/src/allocation.dart:5:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';
^
packages/ffi/src/utf8.dart:23:20: Error: Type 'Struct' not found.
class Utf8 extends Struct {
^^^^^^
packages/ffi/src/utf8.dart:26:21: Error: Type 'Pointer' not found.
static int strlen(Pointer<Utf8> string) {
^^^^^^^
packages/ffi/src/utf8.dart:26:21: Error: Expected 0 type arguments.
static int strlen(Pointer<Utf8> string) {
^
packages/ffi/src/utf8.dart:41:26: Error: Type 'Pointer' not found.
static String fromUtf8(Pointer<Utf8> string) {
^^^^^^^
packages/ffi/src/utf8.dart:41:26: Error: Expected 0 type arguments.
static String fromUtf8(Pointer<Utf8> string) {
^
packages/ffi/src/utf8.dart:54:10: Error: Type 'Pointer' not found.
static Pointer<Utf8> toUtf8(String string) {
^^^^^^^
packages/ffi/src/utf8.dart:54:10: Error: Expected 0 type arguments.
static Pointer<Utf8> toUtf8(String string) {
^
packages/ffi/src/utf16.dart:16:21: Error: Type 'Struct' not found.
class Utf16 extends Struct {
^^^^^^
packages/ffi/src/utf16.dart:24:10: Error: Type 'Pointer' not found.
static Pointer<Utf16> toUtf16(String s) {
^^^^^^^
packages/ffi/src/utf16.dart:24:10: Error: Expected 0 type arguments.
static Pointer<Utf16> toUtf16(String s) {
^
packages/ffi/src/allocation.dart:9:7: Error: Type 'DynamicLibrary' not found.
final DynamicLibrary stdlib = Platform.isWindows
^^^^^^^^^^^^^^
packages/ffi/src/allocation.dart:13:29: Error: Type 'Pointer' not found.
typedef PosixMallocNative = Pointer Function(IntPtr);
^^^^^^^
packages/ffi/src/allocation.dart:13:46: Error: Type 'IntPtr' not found.
typedef PosixMallocNative = Pointer Function(IntPtr);
^^^^^^
packages/ffi/src/allocation.dart:14:23: Error: Type 'Pointer' not found.
typedef PosixMalloc = Pointer Function(int);
^^^^^^^
packages/ffi/src/allocation.dart:18:27: Error: Type 'Void' not found.
typedef PosixFreeNative = Void Function(Pointer);
^^^^
packages/ffi/src/allocation.dart:18:41: Error: Type 'Pointer' not found.
typedef PosixFreeNative = Void Function(Pointer);
^^^^^^^
packages/ffi/src/allocation.dart:19:35: Error: Type 'Pointer' not found.
typedef PosixFree = void Function(Pointer);
^^^^^^^
packages/ffi/src/allocation.dart:23:31: Error: Type 'Pointer' not found.
typedef WinGetProcessHeapFn = Pointer Function();
^^^^^^^
packages/ffi/src/allocation.dart:26:7: Error: Type 'Pointer' not found.
final Pointer processHeap = winGetProcessHeap();
^^^^^^^
And so on.....
Is there any way to configure the compiler to only build the files related to Web or Mobile for the respective runs?
For Reference: Github repo with the error recreated: https://github.com/JoshMarkF/MoorFFIIntegrationDemo
Edit
flutter build web and flutter run -d chrome all works fine.
because web ui code (webui.dart) and mobile ui code (mobileui.dart) are in different dart file,
so these two files can have different import
You can copy paste run 3 three files below, main.dart, webui.dart and mobileui.dart
You can use conditional import to separate different implement for web and mobile
so web and mobile can have totally different logic
code snippet call with prefix multiPlatform
import 'mobileui.dart' if (dart.library.html) 'webui.dart' as multiPlatform;
...
home: multiPlatform.TestPlugin(),
working demo when use Android Studio run with Android Emulator and Chrome
main.dart
import 'package:flutter/material.dart';
import 'mobileui.dart' if (dart.library.html) 'webui.dart' as multiPlatform;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: multiPlatform.TestPlugin(),
);
}
}
mobileui.dart
import 'package:flutter/material.dart';
class TestPlugin extends StatefulWidget {
#override
_TestPluginState createState() => _TestPluginState();
}
class _TestPluginState extends State<TestPlugin> {
#override
Widget build(BuildContext context) {
return Text("Mobile");
}
}
webui.dart
import 'package:flutter/material.dart';
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:ui' as ui;
class TestPlugin extends StatefulWidget {
TestPlugin();
_TestPluginState createState() => _TestPluginState();
}
class _TestPluginState extends State<TestPlugin> {
String createdViewId = 'map_element';
#override
void initState() {
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
createdViewId,
(int viewId) => html.IFrameElement()
..width = MediaQuery.of(context).size.width.toString() //'800'
..height = MediaQuery.of(context).size.height.toString() //'400'
..srcdoc = """<!DOCTYPE html><html>
<head><title>Page Title</title></head><body><h1>This is a Heading</h1><p>This is a paragraph.</p></body></html>"""
/*..src = "http://f12apidev32.umc.com/Tableau/jsapi_practice.aspx"*/
..style.border = 'none');
super.initState();
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey[300], width: 1),
borderRadius: BorderRadius.all(Radius.circular(5))),
width: 200,
height: 200,
child: Directionality(
textDirection: TextDirection.ltr,
child: HtmlElementView(
viewType: createdViewId,
)));
}
}
web_ffi: ^0.7.2 in dependencies in pubspec.yaml
and import this file.
import 'package:web_ffi/web_ffi.dart';
it will separately open web and mobile device..