I'm having an issue when running my build - flutter

So I try building my flutter code and get the error:
"H:/src/flutter/.pub-cache/hosted/pub.dartlang.org/chewie-0.9.10/lib/src/chewie_player.dart:277:51: Error: Too many positional
Try removing the extra positional arguments.
context.dependOnInheritedWidgetOfExactType(_ChewieControllerProvider)"
Here is the code snippet in question
static ChewieController of(BuildContext context) {
final chewieControllerProvider =
context.dependOnInheritedWidgetOfExactType(_ChewieControllerProvider)
as _ChewieControllerProvider;

This seems like a version mismatch between Flutter and chewie_player.
In version 0.9.10, the code is as you have used it.
However, in the latest version, it is,
static ChewieController of(BuildContext context) {
final chewieControllerProvider = context.dependOnInheritedWidgetOfExactType<_ChewieControllerProvider>();
return chewieControllerProvider.controller;
}
which should the correct way according to your flutter version since the function context.dependOnInheritedWidgetOfExactType doesn't take any positional parameters.
Just update your dependency to chewie: ^1.0.0.
This should fix it.

Related

Error: Member not found: 'Scrollable.maybeOf'. final scrollableState = Scrollable.maybeOf(context);

Hello, good day everyone i encountered this error in my Android Studio Can someone help me about this i am on Flutter 3.3.8 Thank you so much
#override
void didChangeDependencies() {
super.didChangeDependencies();
final scrollableState = Scrollable.maybeOf(context);
if (scrollableState != null) {
// The TypeAheadField is inside a scrollable widget
_scrollPosition = scrollableState.position;
_scrollPosition!.removeListener(_scrollResizeListener);
_scrollPosition!.isScrollingNotifier.addListener(_scrollResizeListener);
}
}
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_typeahead-4.3.1/lib/src/flutter_typeahead.dart:898:40: Error: Member not found: 'Scrollable.maybeOf'.
final scrollableState = Scrollable.maybeOf(context);
^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_typeahead-4.3.1/lib/src/cupertino_flutter_typeahead.dart:649:40: Error: Member not found: 'Scrollable.maybeOf'.
final scrollableState = Scrollable.maybeOf(context);
^^^^^^^
I am expecting someone could help me about this flutter and my project, Ill promise after i learn coding i will payback all the help to the one in needs.!!!!
goto path by ctrl+click on path provided in console. change maybeOf to of. Run flutter pub get in terminal and error is gone.
First upgrade your flutter version to latest stable running flutter upgrade
Then, update your flutter_typeahead dependency version to at least 4.3.1 on your pubspec.yaml file.
By the widget change log it states that this has been fixed on 28-January-2023
PR #447 - fix: Use maybeOf for scrollable to not throw an exception in
flutter …
See change log: https://pub.dev/packages/flutter_typeahead/changelog#431---28-january-2023

Why does Android Studio Dart Analysis throws error for required keyword?

I have a Coordinate Class where both parameters are required.
Both Android Studio and Visual Studio Code do not recognize the required keyword.
Why does Dart Analysis give Undefined class required error?
required isn't a type.
Try correcting the name to match an existing type.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
Coordinate coordinate = Coordinate(lon: 3.4, lat: 2.3);
return Container(
child: Text(coordinate.lat.toString()),
);
}
}
class Coordinate {
double lon;
double lat;
Coordinate({required lon, required lat});
}
Dart SDK version: 2.12.3 (stable)
Is the version of Dart updated to 2.12 on your environment?
The second error says required is not a valid keyword, the required keyword was added in Dart 2.12.
Note: The compiler is parsing the required keyword as a type and as it does not find the type required defined anywhere in the project it interprets it is as dynamic, Hence the error.
This is the answer to the question
Why does Android Studio Dart Analysis throws error for required keyword?
Now, If it is still not working for you, I suggest you open a new issue with the appropriate tags about the sdk install/environment issues you have. As this question has served its purpose

Use dependOnInheritedWidgetOfExactType instead inheritFromWidgetOfExactType for flutter constants best practices

I want to implement best practices for constants in Flutter by Rémi Rousselet's: What's the best practice to keep all the constants in Flutter?
but inheritFromWidgetOfExactType is deprecated:
'inheritFromWidgetOfExactType' is deprecated and shouldn't be used.
Use dependOnInheritedWidgetOfExactType instead.
This feature was deprecated after v1.12.1..
Try replacing the use of the deprecated member with the replacement.
Could you help me to implement that with dependOnInheritedWidgetOfExactType?
I use in my class Provider extends InheritedWidget like this
static Bloc of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<Provider>().bloc;
}
Example of a replacement:
// Before, We write :
context.inheritFromWidgetOfExactType(Name) as Name;
// Now instead of taking a Type as argument, The method is a generic.
context.dependOnInheritedWidgetOfExactType<Name>();
A snippet of using the dependOnInheritedWidgetOfExactType method
static Name of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<Name>();
}

Extracting Class Members like Widget Builders to a Different File?

In developing some of the screens for my flutter app, I regularly need to dynamically render widgets based on the state of the screen. For circumstances where it makes sense to create a separate widget and include it, I do that.
However, there are many use cases where what I need to render is not fit for a widget, and leverages existing state from the page. Therefore I use builder methods to render the appropriate widgets to the page. As anyone who uses Flutter knows, that can lead to lengthy code where you need to scroll up/down a lot to get to what you need to work on.
For better maintainability, I would love to move those builder methods into separate files, and then just include them. This would make it much easier to work on specific code widgets rendered and make the screen widget much cleaner.
But I haven't found a proper way to extract that dynamic widget code, which makes use of state, calls to update state, etc. I'm looking for a type of "include" file that would insert code into the main screen and render as if it's part of the core code.
Is this possible? How to achieve?
With the introduction of extension members, I came across this really neat way of achieving exactly what your described!
Say you have a State class defined like this:
class MyWidgetState extends State<MyWidget> {
int cakes;
#override
void initState() {
super.initState();
cakes = 0;
}
#override
Widget build(BuildContext context) {
return Builder(
builder: (context) => Text('$cakes'),
);
}
}
As you can see, there is a local variable cakes and a builder function. The very neat way to extract this builder now is the following:
extension CakesBuilderExtension on MyWidgetState {
Widget cakesBuilder(BuildContext context) {
return Text('$cakes');
}
}
Now, the cakes member can be accessed from the extension even if the extension is placed in another file.
Now, you would update your State class like this (the builder changed):
class MyWidgetState extends State<MyWidget> {
int cakes;
#override
void initState() {
super.initState();
cakes = 0;
}
#override
Widget build(BuildContext context) {
return Builder(
builder: cakesBuilder,
);
}
}
The cakesBuilder can be referenced from MyWidgetState, even though it is only declared in the CakesBuilderExtension!
Note
The extension feature requires Dart 2.6. This is not yet available in the stable channel, but should be around the end of 2019 I guess. Thus, you need to use the dev or master channels: flutter channel dev or flutter channel master and update the environment constraint in your pubspec.yaml file:
environment:
sdk: '>=2.6.0-dev.8.2 <3.0.0'

Use multiple LocalizationsDelegates in Flutter

I'm facing an issue where I'm trying to use multiple LocalizationsDelegates in a MaterialApp.
I'm using the Dart intl tools to provide translations to my labels. When I have multiple LocalizationsDelegates only the one that is specified the first gets the translated values. The labels of the next delegate, get the default value provided in the Intl.message() function.
Short, self contained, correct example
I've set up a minimal project as an example of this issue on GitHub.
Code snippets
In the MaterialApp, I define a bunch of localizationsDelegates, including two app specific ones: DogLocalizationsDelegate and CatLocalizationsDelegate.
MaterialApp(
// other properties
locale: Locale("en"),
localizationsDelegates: [
CatLocalizationsDelegate(),
DogLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en'),
const Locale('nl'),
],
);
The delegates have the same boilerplate code, but provide different labels.
Here's how the DogLocalizations and its DogLocalizationsDelegate look like.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'messages_all.dart';
class DogLocalizations {
static Future<DogLocalizations> load(Locale locale) {
final String name = locale.languageCode;
final String localeName = Intl.canonicalizedLocale(name);
return initializeMessages(localeName).then((_) {
Intl.defaultLocale = localeName;
return DogLocalizations();
});
}
static DogLocalizations of(BuildContext context) {
return Localizations.of<DogLocalizations>(context, DogLocalizations);
}
String get bark {
return Intl.message(
'<insert dog sound>',
name: 'bark',
);
}
}
class DogLocalizationsDelegate extends LocalizationsDelegate<DogLocalizations> {
const DogLocalizationsDelegate();
#override
bool isSupported(Locale locale) => ['en', 'nl'].contains(locale.languageCode);
#override
Future<DogLocalizations> load(Locale locale) => DogLocalizations.load(locale);
#override
bool shouldReload(DogLocalizationsDelegate old) => false;
}
The CatLocalizations are the same, but with a meow String getter. Full example in the GitHub project.
Commands used to generate translations files
I'm using multiple extraction and generation commands instead of having multiple files in one command. This is because I'm actually having this problem with a library (with its own labels) and a consumer of that library (that also has its own labels).
Extract labels of both cats and dogs
flutter pub run intl_translation:extract_to_arb --output-dir=lib/cat_labels/gen lib/cat_labels/CatLabels.dart
flutter pub run intl_translation:extract_to_arb --output-dir=lib/dog_labels/gen lib/dog_labels/DogLabels.dart
Translate the generated intl_messages.arb to have two language files
intl_en.arb
intl_nl.arb
And then add the correct translated values to these files.
Generate the dart files from ARB
flutter pub run intl_translation:generate_from_arb --output-dir=lib/cat_labels lib/cat_labels/CatLabels.dart lib/cat_labels/gen/intl_*.arb
flutter pub run intl_translation:generate_from_arb --output-dir=lib/dog_labels lib/dog_labels/DogLabels.dart lib/dog_labels/gen/intl_*.arb
Issue
In this demo project, having the following order of delegates:
// main.dart (line 20)
DogLocalizationsDelegate(),
CatLocalizationsDelegate(),
will give the translation for the bark label, but not for the meow label.
When switching it:
// main.dart (line 20)
CatLocalizationsDelegate(),
DogLocalizationsDelegate(),
will give the translation for the meow label, but not for the bark label.
Why multiple localization delegates
In case you're wondering why: I'm using labels in a library and in the consumer apps of that library.
Important to know is that it's not (really) possible to specify both localization files in the same generator command because of this.
From what I learned, no, you can’t use multiple localizations delegates like this. That is because intl’s initializeMessages can only be called once per locale.
So in your example, once your CatLocalizationsDelegate runs initializeMessages, the one for DogLocalizationsDelegate has no effect. That’s why you are only seeing the translation Meow! but not Dog’s, or whichever one gets to run it first.
For additional reading, check out https://phrase.com/blog/posts/how-to-internationalize-a-flutter-app/ and please share feedback at https://github.com/flutter/flutter/issues/41437.
I find a solution with a dedicated flutter package available here: [https://pub.dev/packages/multiple_localization][1]
You can use the multiple_localizations package. it worked for me