Issues and errors with Image Picker plugin for Flutter - flutter

I'm new to Dart and Flutter.
I'm trying to run the example code for ImagePicker from Pub.Dev (https://pub.dev/packages/image_picker). I'm starting with the example code provided in the ReadMe tab. I'm using image_picker: ^0.7.5+3 within the dependencies in pubspec.yaml
What I want to happen: in an IOS Simulator, when I select an image from the gallery, the selected image should show up in the body of the application. Using the example code from Pub.Dev ReadMe tab, the body shows the text: "No image selected." However, even if I select an image, the image is not shown, and the body text does not get replaced with the selected image.
Just by simply copy-pasting the example code from PubDev, I get two errors:
for File _image; => i get this error
Error: Non-nullable instance field '_image' must be initialized.
Try adding an initializer expression, or a generative constructor that initializes it, or mark it 'late'.
So, i tried to use instead: File? _image
However, the following error results in the line => : Image.file(_image),
Error: The argument type 'File?' can't be assigned to the parameter type 'File'.
So I change the line to: : Image.file(_image!),
This removes the error, but the code doesn't seem to work (the selected image does not get shown in the body of the application).
Please help?
This is the full code I'm using with the modification I've outlined above:
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget { #override Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
); } }
class MyHomePage extends StatefulWidget { #override
_MyHomePageState createState() => _MyHomePageState(); }
class _MyHomePageState extends State<MyHomePage> { File? _image; final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
}); }
#override Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Picker Example'),
),
body: Center(
child: _image == null
? Text('No image selected.')
: Image.file(_image!),
),
floatingActionButton: FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
); } }
Other details:
pubspec.yaml
dependencies:
flutter:
sdk: flutter
image_picker: ^0.7.5+3
cupertino_icons: ^1.0.2
Flutter Doctor results (I'm using an IOS simulator, so I assume the issue with the Android toolchain has no impact?:
[flutter] flutter doctor -v [✓] Flutter (Channel stable, 2.2.0, on
macOS 11.3.1 20E241 darwin-arm, locale en-GB)
• Flutter version 2.2.0 at /Users/JM/flutter
• Framework revision b22742018b (13 days ago), 2021-05-14 19:12:57 -0700
• Engine revision a9d88a4d18
• Dart version 2.13.0
[!] Android toolchain - develop for Android devices (Android SDK
version 30.0.3)
• Android SDK at /Users/JM/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
✗ Android license status unknown.
Run flutter doctor --android-licenses to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for
more details.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.5, Build version 12E262
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
[✓] VS Code (version 1.56.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.22.0
[✓] Connected device (2 available)
• iPhone 12 Pro Max (mobile) • 1B078BE2-30D5-4972-A6DF-2748712A8641 • ios •
com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
• Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212
! Doctor found issues in 1 category. exit code 0

Your code is working perfectly with my android simulator.
I'm unfortunately not able to test for iOS currently as i have to renew my Macbook which is too old to get XCode 12.
Anyway, the Android license issue you have cannot have any impact for your refreshing screen issue, indeed.

Related

Application UI is completely distorted when Mobile Phone Display Text Size is enlarged - Not Responsive

When the user increases the Display Font Size of mobile phone Android/iOS the application UI is completed broken.
Requirement: I need to fix the size of all the TextFields though the user increases the display font size. (Give no support for Accessibility)
Note: I have already given "fontSize" to my textfields, though it enlarges when font size is increase.
Steps to reproduce it:
iOS Mobile: Settings -> Accessibilities -> Display and Text Size -> Large Text -> On -> Maximum
Android Mobile: Settings -> Display -> Font Size -> Huge
Please let me know if any solution prevails.
Help will be appreciated.
Here is my flutter doctor -v
Flutter (Channel stable, 1.22.6, on macOS 11.2.1 20D74 darwin-arm, locale en-IN)
• Flutter version 1.22.6 at /Users/sunflower/Documents/flutter
• Framework revision 9b2d32b (3 months ago), 2021-01-22 14:36:39 -0800
• Engine revision 2f0af37152
• Dart version 2.10.5
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/sunflower/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /Users/sunflower/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.4)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.4, Build version 12D4e
• CocoaPods version 1.10.1
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin installed
• Dart plugin version 201.9317
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
Please find the attached files to get the highlight of the issue.
When Font Size is increased Application UI
Everything is overflowed
this might help, font size change prohibited,
MaterialApp(
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
child: child,
);
},
title: 'Home Page',
);
Lock the textScaleFactor to 1.0 in your 'MaterialApp`:
MaterialApp(
title: 'My App',
builder: (BuildContext context, Widget _) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), // like this
child: Home(), // Your widgets go here
);
},
);

Flutter webview package inline media problem

webview_flutter 1.0.7 using your package.
https://github.com/flutter/plugins/pull/3334/commits/1d7e5ebab3c86bf5f2bf252ae14bca87be33c414
I made the changes on the page. Although I made all the changes, the ipad is working in full screen but the iphone does not work
sample videos as below
https://user-images.githubusercontent.com/57063848/104122686-8abf8700-5357-11eb-96b4-beb18cc5c4e1.mov
https://user-images.githubusercontent.com/57063848/104122691-901cd180-5357-11eb-9dcc-aa6b842263de.mov
sample code
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Welcome to Flutter',
home: Scaffold(
body: WebView(
initialUrl: "https://www.medivizyon.com.tr/in5",
javascriptMode: JavascriptMode.unrestricted,
initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow,
allowsInlineMediaPlayback: true,
),
),
);
}
flutter doctor -v
[✓] Flutter (Channel stable, 1.22.5, on Mac OS X 10.15.7 19H2 darwin-x64, locale tr-TR)
• Flutter version 1.22.5 at /Users/gokhan/flutter
• Framework revision 7891006299 (4 weeks ago), 2020-12-10 11:54:40 -0800
• Engine revision ae90085a84
• Dart version 2.10.4
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Users/gokhan/Library/Android/sdk
• Platform android-29, build-tools 29.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.3, Build version 12C33
• CocoaPods version 1.10.0
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 46.0.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.52.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.18.1
pubspec.yaml
version: 1.0.0+1
environment:
sdk: ">=2.8.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
webview_flutter: ^1.0.7
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
pythoneer commented yesterday
What is the current state of this issue? Flutter 2 is stable and released and the same is true for webview_flutter in version 2.0.2 . I can see changes being made to allow this
} else if ([key isEqualToString:#"allowsInlineMediaPlayback"]) {
NSNumber* allowsInlineMediaPlayback = settings[key];
_webView.configuration.allowsInlineMediaPlayback = [allowsInlineMediaPlayback boolValue];
} else {
But i don't have the desired effect. It is still not working as described in this issue. If i fork this and apply the changes manually
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
[self updateAutoMediaPlaybackPolicy:args[#"autoMediaPlaybackPolicy"]
inConfiguration:configuration];
configuration.allowsInlineMediaPlayback = true;
it is working as expected.

Assertion fails on Web version, but succeeds on iOS Flutter

I have a different behaviour when I run my app on Chrome then when I run it on iOS. It happens when I get to this screen:
import 'dart:io';
import 'package:fixit_shop_flutter/fixit_shop_app/authentication_bloc/user_repository.dart';
import 'package:fixit_shop_flutter/fixit_shop_app/register/bloc/register_bloc.dart';
import 'package:fixit_shop_flutter/fixit_shop_app/register/register_form.dart';
import 'package:fixit_shop_flutter/localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class RegisterScreen extends StatelessWidget {
final UserRepository _userRepository;
RegisterScreen({Key key, #required UserRepository userRepository})
: assert(userRepository != null),
_userRepository = userRepository,
super(key: key);
#override
Widget build(BuildContext context) {
dynamic backButton =
Platform.isIOS ? CupertinoIcons.back : Icons.arrow_back;
return Stack(
children: [
Image.asset('assets/mainBg.png',
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover),
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
centerTitle: true,
elevation: 0,
title: Text(
AppLocalizations.instance.text('RegisterScreenTitle'),
// textAlign: TextAlign.center,
style: TextStyle(
color: Colors.orange,
fontSize: 22,
fontWeight: FontWeight.w500,
letterSpacing: 1),
),
backgroundColor: Colors.transparent,
leading: IconButton(
icon: Icon(backButton),
color: Colors.redAccent,
onPressed: () {
Navigator.pop(context);
}),
),
body: BlocProvider<RegisterBloc>(
create: (context) => RegisterBloc(userRepository: _userRepository),
child: RegisterForm(),
),
),
],
);
}
}
If on iOS the screen loads normally, while when running the web version it throws the Another exception was thrown: Failed assertion: boolean expression must not be null error.
Do you now what could cause this different behaviour? I followed all the widgets that pass the userRepository along the tree and is all good..as in fact on iOS runs properly.
Here is my flutter doctor :
[✓] Flutter (Channel unknown, v1.12.13+hotfix.5, on Mac OS X 10.13.6 17G65, locale en-IT)
• Flutter version 1.12.13+hotfix.5 at /Users/vinnytwice/Developer/flutter
• Framework revision 27321ebbad (5 months ago), 2019-12-10 18:15:01 -0800
• Engine revision 2994f7e1e6
• Dart version 2.7.0
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at /Users/vinnytwice/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.2.1, Build version 10E1001
• CocoaPods version 1.8.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 3.5)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 44.0.1
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] Connected device (3 available)
• iPad Pro (10.5-inch) • 23C29147-A4F3-4B9F-8182-9C813D5A54AD • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)
• Chrome • chrome • web-javascript • Google Chrome 81.0.4044.129
• Web Server • web-server • web-javascript • Flutter Tools
• No issues found!
Do you see anything to update? I'm on High Sierra so I think I can't go above Flutter version 1.12.13+hotfix.5. Also I see that the channel is unknown..could it have something to do with it ?
As always many thanks for your time and help.
Cheers.
found the reason.. it wasn't the assertion but the platform check I was doing on backButton, that only included iOS..I added the check for the web and it now works es expected. I'll leave the question up as might help others porting their apps to web.
dynamic backButton;
if (kIsWeb) {
backButton = Icons.arrow_back;
} else if (Platform.isIOS) {
backButton = CupertinoIcons.back;
} else {
backButton = Icons.arrow_back;
}

What is _CompactLinkedHashSet in flutter?

I am trying to run my first flutter app and I am getting a redscreen error when I run it on my iPhone but it runs without a problem on the simulator:
type '_CompactLinkedHashSet' is not a subtype of type 'Widget'.
Does anyone know what this error refers to? The error-causing widget is a FutureBuilder. The type of the FutureBuilder is the same as the Future.
Flutter doctor:
[✓] Flutter (Channel dev, v1.18.0, on Mac OS X 10.15.4 19E287, locale en-AU)
• Flutter version 1.18.0 at /Users/Josh/Developer/flutter
• Framework revision 8f7327f83a (11 days ago), 2020-04-06 22:11:01 -0400
• Engine revision 49891e0653
• Dart version 2.8.0 (build 2.8.0-dev.20.0 1210d27678)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Users/Josh/Library/Android/sdk
• Platform android-29, build-tools 29.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 11.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.4.1, Build version 11E503a
• CocoaPods version 1.9.1
[✓] Android Studio (version 3.6)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 45.1.1
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
[!] Connected device
! No devices available
! Doctor found issues in 1 category.
class DisclaimerData {
bool agreed;
String version;
DisclaimerData({this.agreed, this.version});
}
class DisclaimerView extends StatefulWidget {
#override
_DisclaimerViewState createState() => _DisclaimerViewState();
}
class _DisclaimerViewState extends State<DisclaimerView> {
Future<DisclaimerData> _getAgreed() async {
final preferences = await SharedPreferences.getInstance();
final disclaimerValues = DisclaimerData();
disclaimerValues.agreed = preferences.getBool('disclaimer_agreed') ?? false;
disclaimerValues.version =
preferences.getString('disclaimer_version') ?? '0';
return disclaimerValues;
}
#override
Widget build(BuildContext context) {
return FutureBuilder<DisclaimerData>(
future: _getAgreed(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Scaffold(
...
_CompactLinkedHashSet is an alternative name for Set. It is internal default Set implementation that dart uses to optimize Set's data structure.
_CompactLinkedHashSet (default Set implementation) and _InternalLinkedHashMap (default Map) have good asymptotic storage efficiency as the size of the collection grows. They are always better than conveniently available alternatives above 10 elements and are designed for a better locality.
source:
https://github.com/dart-lang/sdk/issues/26081

Images are not visible during flutter_driver tests

When running UI tests with flutter driver the assets are not displayed on iPhone simulator. They are displayed when app is run in debug mode from VS Code.
I tried to reproduce it on the example project generated by flutter create -i swift -a kotlin --androidx --org com.example example but without success. I followed exactly the same process of introducing flutter_driver tests in both apps according to this article. Unfortunately, in my app the images are not shown, and in example app they appear just fine. I compared project.pbxproj and pubspec.yaml files and they look fine to me.
For instance my pubspec.yaml:
dev_dependencies:
flutter_driver:
sdk: flutter
test: any
flutter_test:
sdk: flutter
screenshots:
flutter:
uses-material-design: true
assets:
- assets/lang/pl.json
- assets/images/
Is there anything that can help me with this issue? What can I do to debug the problem?
Logs
Beware, this is a very long log output.
flutter drive --target=test_driver/app.dart --verbose
https://gist.github.com/orestesgaolin/861c4f191773ca152b400d97ced2daeb
Flutter doctor
flutter doctor -v
[✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.14.6 18G87, locale pl-PL)
• Flutter version 1.7.8+hotfix.4 at /Users/dominik/Library/flutter
• Framework revision 20e59316b8 (6 weeks ago), 2019-07-18 20:04:33 -0700
• Engine revision fee001c93f
• Dart version 2.4.0
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/dominik/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• ANDROID_SDK_ROOT = /Users/dominik/Library/Android/sdk
• Java binary at: /Users/dominik/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/191.5791312/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 10.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.3, Build version 10G8
• CocoaPods version 1.7.5
[✓] iOS tools - develop for iOS devices
• ios-deploy 1.9.4
[✓] Android Studio (version 3.5)
• Android Studio at /Users/dominik/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/191.5791312/Android Studio.app/Contents
• Flutter plugin version 37.1.1
• Dart plugin version 183.6270
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] VS Code (version 1.37.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.3.0
[✓] Connected device (1 available)
• iPhone Xs Max • C1A5534A-AE3A-4FB3-95F6-993E351FF2D3 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-4 (simulator)
• No issues found!
So the solution was to create separate main_test.dart file that wraps whole app in DefaultAssetBundle like so:
runApp(DefaultAssetBundle(
bundle: TestAssetBundle(),
child: App(),
));
and TestAssetBundle is like so:
class TestAssetBundle extends CachingAssetBundle {
#override
Future<String> loadString(String key, {bool cache = true}) async {
final ByteData data = await load(key);
if (data == null) throw FlutterError('Unable to load asset');
return utf8.decode(data.buffer.asUint8List());
}
#override
Future<ByteData> load(String key) async => rootBundle.load(key);
}
And in app.dart you have to reference correct file:
import 'package:flutter_driver/driver_extension.dart';
import 'package:playoo_league_player/main_test.dart' as app;
void main() {
// This line enables the extension.
enableFlutterDriverExtension();
// Call the `main()` function of the app, or call `runApp` with
// any widget you are interested in testing.
app.main();
}
Full explanation can be found here: https://medium.com/#sardox.vl/flutter-test-and-randomly-missing-assets-in-goldens-ea959cdd336a
Those Images are not loading because of cache issues .For that we can
use
PRE CACHE IMAGES to preload the images
class App extends StatefulWidget {
#override
State<App> createState() => _AppState();
}
class _AppState extends State<App>{
late image image1
late image image2
#override
void initState() {
super.initState();
image1 = Image.asset('assets/images/');
image2 = Image.asset('assets/images/');
}
#override
void didChangeDependencies() {
pre cacheImage(image1.image, context);
pre cacheImage(image2.image, context);
super.didChangeDependencies();
}
#override
Widget build(BuildContext context) {
return Container
}
if it is multiple assets or images you can save this method as class and
initialise it from main.dart .