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..
Related
I am trying to use generic with interface in dart lang in flutter framework.
Basically, I would like to set the type of the generic to an implementation of ITestClass.
in the typedef definition, I am setting the "T" type to extend ITestClass (because you cannot implement with generic in dart). But when I actually use this typedef in the build method, it throws following error
The argument type 'TestClass' can't be assigned to the parameter type 'T'.
From my knowledge, TestClass is an implementation of ITestClass so type error should not be thrown. How can I overcome this problem?
import 'package:flutter/material.dart';
class ITestClass {}
class TestClass implements ITestClass {}
typedef TestTypedef<T extends ITestClass> = Widget Function(T model);
void main() {
runApp(
MaterialApp(
home: TestWidget(
testTypedef: (model) => Container(),
),
),
);
}
class TestWidget<T extends ITestClass> extends StatelessWidget {
final TestTypedef<T> testTypedef;
TestWidget({
required this.testTypedef,
});
Widget build(BuildContext context) {
return Scaffold(
body: testTypedef(
TestClass(), // error here
),
);
}
}
error image
I am trying to run this code but getting errors, can anyone help? I have tried making strings, but still, I get the same error.
error: The argument type 'String' can't be assigned to the parameter type 'Uri'. (argument_type_not_assignable at [world_time] lib\pages\loading.dart:17)enter image description here
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'dart:convert';
import 'dart:core';
class Loading extends StatefulWidget {
#override
_LoadingState createState() => _LoadingState();
}
class _LoadingState extends State<Loading> {
void getData() async {
Response response = await get(
'https://jsonplaceholder.typicode.com/todos/1');
print(response.body);
}
#override
void initState() {
super.initState();
getData();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('loading screen'),
);
}?
}
Do
get(Uri.parse('https://jsonplaceholder.typicode.com/todos/1'));
OR
get(Uri.https('jsonplaceholder.typicode.com', '/todos/1'));
You can solve the problem by using this:
Uri.parse('https://jsonplaceholder.typicode.com/todos/1')
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.
It's actually very simple app, only asking for location. I searched a lot about this, very speculative matter everyone says different things and I couldn't solve myself yet. What I want is, I want to ask user of location permission. I added this into dependencies in pubspec.yaml:
permission_handler: ^5.0.1+1
And in AndroidManifest.xml I added these:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-permission>
And then I've written my main.dart, just like that. Didn't do anything else:
import 'package:flutter/material.dart';
import 'package:permission_handler/permiission_handler.dart';
void main() => runApp(MaterialApp(home: MyHome()));
class _MyHomeState extends StatefulWidget {
#override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
PermissionStatus _status;
#override
void initState() {
super.initState();
PermissionHandler()
.checkPermissionStatus(PermissionGroup.locationWhenInUse)
.then(_updateStatus);
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Column(
children: <Widget>[
Text('$_status'),
SizedBox(height: 60),
RaisedButton(
child: Text('Ask Permission'),
onPressed: _askPermission,
)
], //<Widget>[]
),
); //Column, SafeArea
}
void _updateStatus(PermissionStatus status) {
if (status != _status) {
setState(() {
_status = status;
});
}
}
void _askPermission() {
PermissionHandler().requestPermissions(
[PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
}
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
final status = statuses[PermissionGroup.locationWhenInUse];
//if (status != PermissionStatus.granted) {
//PermissionHandler().openAppSettings();
//} else { //Ignore this, this isn't important.
_updateStatus(status);
}
}
}
Then I use flutter run and it gives lots of error. Like:
Invalid depfile: /home/xx/AndroidStudioProjects/flutter_app/.dart_tool/flutter_build/e9846fe3c39761c4693f6682f4c69be2/kernel_snapshot.d
Invalid depfile: /home/xxx/AndroidStudioProjects/flutter_app/.dart_tool/flutter_build/e9846fe3c39761c4693f6682f4c69be2/kernel_snapshot.d
Error: Could not resolve the package 'permission_handler' in 'package:permission_handler/permiission_handler.dart'.
lib/main.dart:11:7: Error: '_MyHomeState' is already declared in this scope.
class _MyHomeState extends State<MyHome> {
^^^^^^^^^^^^
lib/main.dart:6:7: Context: Previous declaration of '_MyHomeState'.
class _MyHomeState extends StatefulWidget {
^^^^^^^^^^^^
lib/main.dart:59:1: Error: Expected a declaration, but got '}'.
}
^
lib/main.dart:2:8: Error: Not found: 'package:permission_handler/permiission_handler.dart'
import 'package:permission_handler/permiission_handler.dart';
^
lib/main.dart:8:3: Error: '_MyHomeState' isn't a type.
_MyHomeState createState() => _MyHomeState();
^^^^^^^^^^^^
lib/main.dart:8:3: Context: This isn't a type.
_MyHomeState createState() => _MyHomeState();
^^^^^^^^^^^^
lib/main.dart:11:34: Error: Type 'MyHome' not found.
class _MyHomeState extends State<MyHome> {
^^^^^^
lib/main.dart:12:3: Error: Type 'PermissionStatus' not found.
PermissionStatus _status;
^^^^^^^^^^^^^^^^
lib/main.dart:38:22: Error: Type 'PermissionStatus' not found.
void _updateStatus(PermissionStatus status) {
^^^^^^^^^^^^^^^^
lib/main.dart:51:31: Error: Type 'PermissionGroup' not found.
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
^^^^^^^^^^^^^^^
lib/main.dart:51:48: Error: Type 'PermissionStatus' not found.
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
^^^^^^^^^^^^^^^^
lib/main.dart:4:41: Error: Method not found: 'MyHome'.
void main() => runApp(MaterialApp(home: MyHome()));
^^^^^^
lib/main.dart:8:33: Error: Can't use '_MyHomeState' because it is declared more than once.
_MyHomeState createState() => _MyHomeState();
^
lib/main.dart:12:3: Error: 'PermissionStatus' isn't a type.
PermissionStatus _status;
^^^^^^^^^^^^^^^^
lib/main.dart:17:5: Error: The method 'PermissionHandler' isn't defined for the class '_MyHomeState#1'.
- '_MyHomeState#1' is from 'package:flutter_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'PermissionHandler'.
PermissionHandler()
^^^^^^^^^^^^^^^^^
lib/main.dart:18:32: Error: The getter 'PermissionGroup' isn't defined for the class '_MyHomeState#1'.
- '_MyHomeState#1' is from 'package:flutter_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'PermissionGroup'.
.checkPermissionStatus(PermissionGroup.locationWhenInUse)
^^^^^^^^^^^^^^^
lib/main.dart:38:22: Error: 'PermissionStatus' isn't a type.
void _updateStatus(PermissionStatus status) {
^^^^^^^^^^^^^^^^
lib/main.dart:47:5: Error: The method 'PermissionHandler' isn't defined for the class '_MyHomeState#1'.
- '_MyHomeState#1' is from 'package:flutter_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'PermissionHandler'.
PermissionHandler().requestPermissions(
^^^^^^^^^^^^^^^^^
lib/main.dart:48:10: Error: The getter 'PermissionGroup' isn't defined for the class '_MyHomeState#1'.
- '_MyHomeState#1' is from 'package:flutter_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'PermissionGroup'.
[PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
^^^^^^^^^^^^^^^
lib/main.dart:51:31: Error: 'PermissionGroup' isn't a type.
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
^^^^^^^^^^^^^^^
lib/main.dart:51:48: Error: 'PermissionStatus' isn't a type.
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
^^^^^^^^^^^^^^^^
lib/main.dart:52:29: Error: The getter 'PermissionGroup' isn't defined for the class '_MyHomeState#1'.
- '_MyHomeState#1' is from 'package:flutter_app/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'PermissionGroup'.
final status = statuses[PermissionGroup.locationWhenInUse];
There are a few problems with your code:
package:permission_handler/permiission_handler.dart Should be: package:permission_handler/permission_handler.dart
class _MyHomeState extends StatefulWidget {
#override
_MyHomeState createState() => _MyHomeState();
}
Should be:
class MyHome extends StatefulWidget {
#override
_MyHomeState createState() => _MyHomeState();
}
There's an extra bracket at the end of the file.
Follows the full corrected source code:
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: MyHome()));
class MyHome extends StatefulWidget {
#override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
PermissionStatus _status;
#override
void initState() {
super.initState();
PermissionHandler()
.checkPermissionStatus(PermissionGroup.locationWhenInUse)
.then(_updateStatus);
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Column(
children: <Widget>[
Text('$_status'),
SizedBox(height: 60),
RaisedButton(
child: Text('Ask Permission'),
onPressed: _askPermission,
)
], //<Widget>[]
),
); //Column, SafeArea
}
void _updateStatus(PermissionStatus status) {
if (status != _status) {
setState(() {
_status = status;
});
}
}
void _askPermission() {
PermissionHandler().requestPermissions(
[PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
}
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
final status = statuses[PermissionGroup.locationWhenInUse];
//if (status != PermissionStatus.granted) {
//PermissionHandler().openAppSettings();
//} else { //Ignore this, this isn't important.
_updateStatus(status);
}
}
You have a typo: package:permission_handler/permiission_handler.dart.
Too many I's :).
I'm using the auto_route package in order to route my app and until 2 days ago everything worked just fine, until now.
For some reason, I'm getting the following error.
SYSTEM:
flutter: 1.22
dart: 2.10.0
auto_route: ^0.6.7
ERROR:
../../.pub-cache/hosted/pub.dartlang.org/custom_navigator-0.3.0/lib/custom_navigator.dart:60:7: Error: The non-abstract class '_CustomNavigatorState' is missing implementations for these members:
- WidgetsBindingObserver.didPushRouteInformation
Try to either
- provide an implementation,
- inherit an implementation from a superclass or mixin,
- mark the class as abstract, or
- provide a 'noSuchMethod' implementation.
class _CustomNavigatorState extends State<CustomNavigator>
^^^^^^^^^^^^^^^^^^^^^
/opt/flutter/packages/flutter/lib/src/widgets/binding.dart:122:16: Context: 'WidgetsBindingObserver.didPushRouteInformation' is defined here.
Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
main.dart
import 'package:device_simulator/device_simulator.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'Test.gr.dart' as r;
void main() => runApp(MyApp());
const bool debugEnableDeviceSimulator = true;
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
builder: ExtendedNavigator.builder<r.Router>(
router: r.Router(),
builder: (context, extendedNav) => DeviceSimulator(
enable: debugEnableDeviceSimulator,
child:
Scaffold(body: extendedNav, backgroundColor: Colors.red ),
),
),
);
}
}
Also, I should mention, because of some update of flutter I guess, now I need to use r.Router instead of just Router.
Test.dart
import 'package:auto_route/auto_route_annotations.dart';
#MaterialAutoRouter(
routes: <AutoRoute>[],
)
class $Router {}
And also if has any importance here it's the generated file
Test.gr.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
// **************************************************************************
// AutoRouteGenerator
// **************************************************************************
// ignore_for_file: public_member_api_docs
import 'package:auto_route/auto_route.dart';
class Routes {
static const all = <String>{};
}
class Router extends RouterBase {
#override
List<RouteDef> get routes => _routes;
final _routes = <RouteDef>[];
#override
Map<Type, AutoRouteFactory> get pagesMap => _pagesMap;
final _pagesMap = <Type, AutoRouteFactory>{};
}
Do you have any idea what's going one here or how can I fix this? Or if I can't fix this, what can I use instead of the auto_route package which will offer me the same benefice?
i solved this problem with add some function on library CustomNavigator :
Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
return didPushRoute(routeInformation.location);
}
Find the place where the error is generating. Add the following function below there. I did the same and my problem is solved.
Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
return didPushRoute(routeInformation.location);
}