The getter 'Share' isn't defined for the class '_SettingsState' - flutter

I have the "share_plus" package, which works fine on emulator. I imported the package as usual, I also tried the functionality to share the actual app without any problem. But when i try to run with the Xcode in real device it gives me this error:
import 'package:share_plus/share_plus.dart';
^
lib/Settings.dart:138:19: Error: The getter 'Share' isn't defined for the class '_SettingsState'.
- '_SettingsState' is from 'package:flutter_projects/Settings.dart' ('lib/Settings.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'Share'.
Share.share('https://apps.apple.com/.....');
What could be the problem?
This is my code:
SettingsSection(
title: 'Extra',
tiles: [
SettingsTile(
title: ('Condividi').tr(), leading: Icon(Icons.share),
onPressed: (context) async {
//share_plus requires iPad users to provide the sharePositionOrigin parameter.
final box = context.findRenderObject() as RenderBox?;
await Share.share('https://apps.apple....,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);
}),
],
),

Related

The method 'FlatButton' isn't defined for the class 'PlatformButton' | showing when using enough_giphy_flutter package

when using emoji_picker and enough_giphy_flutter packages, It shows error like this,
The method 'FlatButton' isn't defined for the class 'PlatformButton'
Try correcting the name to the name of an existing method,
or defining a method named 'FlatButton'. child: FlatButton( ^^^^^^^^^^
I think It is because - the latest version of Flutter, Flatbutton is deprecated and uses TextButton.(https://docs.flutter.dev/release/breaking-changes/buttons#context)
But, How do I use these packages in the latest version of Flutter?
my code here : used emoji_picker package
Widget showEmojiPicker() {
return EmojiPicker(
rows: 4,
columns: 7,
onEmojiSelected: (emoji, category) {
print(emoji);
},
);
}

Flutter : How to close alertdialog from another file

I have 2 dart file. I want close the alertdialog from another dart file in specific conditions.
How can I ?
for example the function i want.(I wrote to express myself better.)
void example(){
if(control==false){
alertDialog.close();
}
Alert dialog file
class AlertForm extends StatefulWidget {
#override
_AlertFormState createState() => _AlertFormState();
}
class _AlertFormState extends State<AlertForm> {
void _showDialog() {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Alert Dialog title"),
content: new Text("Alert Dialog body"),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
So what I want is to use the pop action in a function in another flutter file.
Call Navigator.of(context).pop(); inside the if block (of the void example function of the other Dart file) and the alert should close.
But here is the problem, the context above might not be part of or aware of the dialog and the above may not work.
More info about your setup would suggest a solution to this problem. Like how exactly does the other Dart file exist relative to the file where _showDialog is declared? If it's something you can easily provide context from AlertForm Dart file to the closingDialog Dart file, then you should know what to do. If that's not the case then a navigator key should help.
You can keep a navigator key for the entire application. Then instead of closing the dialog with Navigator.of(context).pop();, you use navigatorKey.currentState!.pop();.
You should have properly setup the navigator key yourself. Refer to this Stackoverflow answer for how to do that: https://stackoverflow.com/a/72945522/13644299
It is not compulsory to use get_it package. You can simply export the NavigationService (that will keep the navigatorKey) from any Dart file or with any method you deem fit, depending on your current state management architecture.

flutter ElevatedButton problems

Here is my code
ElevatedButton(
child : Text ("Continue"),
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
KeyboardUtil.hideKeyboard(context);
Navigator.pushNamed(context, LoginSuccessScreen.routeName);
}
},
),
ElevatedButton isn't a function. (Documentation) Try correcting the name to match an existing function, or define a method or function named 'ElevatedButton'.
The name ElevatedButton is defined in the libraries package:fitnessapp/components/elevated_button.dart and package:flutter/src/material/elevated_button.dart (via package:flutter/material.dart). (Documentation) Try using 'as prefix' for one of the import directives, or hiding the name from all but one of the imports.
"How i can fix this problem" ?
In the dart file you use, there are 2 imported files that have the same name, so it does not know which to use. Either remove one of them or name the libraries:
import 'package:fitnessapp/components/elevated_button.dart' as MyButton;
Then whenever you want to use something from this file, you can call it like:
MyButton.ElevatedButton();

after changing my flutter channel from beta to stable , error started coming up

I moved to stable flutter channel from beta, after upgrading flutter,
I am getting error for ListTile() attributes and ScaffoldMassanger,
child: ListTile(
horizontalTitleGap: 10, // error
minVerticalPadding: 10, // error
),
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('success'),
duration: Duration(seconds: 2),
));
I tried below solutions:
flutter upgrade
flutter clean
flutter pub get
reinstalled dart and flutter plugin in VSCode
flutter run
updated vscode
no success
error log after removing depreciated attributes,
lib/widgets/list_expense.dart:32:9: Error: The getter 'ScaffoldMessenger' isn't defined for the class '_ListExpenseState'.
'_ListExpenseState' is from 'package:XpenseTracker/widgets/list_expense.dart' ('lib/widgets/list_expense.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'ScaffoldMessenger'.
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
These properties have been removed, as can be seen in the class (ListTile) documentation:
https://api.flutter.dev/flutter/material/ListTile/ListTile.html
Please take a look at this package here, in order to use these properties:
https://pub.dev/packages/list_tile_more_customizable
[EDIT]
For your Scaffold issue please try to use a static helper function like so, then pass the string to render, along with the BuildContext:
static Future showSimpleSnackBar(
String message, GlobalKey<ScaffoldState> contextState) async {
final snackBar = SnackBar(
content: Text(message),
duration: Duration(seconds: 3),
action: SnackBarAction(
label: "Got it",
onPressed: () {
//invoke an action here...
},
),
);
contextState.currentState.removeCurrentSnackBar();
contextState.currentState.showSnackBar(snackBar);
}
Make sure as well that the BuildContext is coming from a Global ScaffoldKey, and that the scaffold will be responsible with rendering the snackbar.

The named parameter onImageTaped isn't defined

I want to implement a functionality where if an image is clicked, then it should navigate to a new page. Below code was working perfectly until recently when it showed me a squiggly red error underneath the function onImageTap.
child: new Carousel(
boxFit: BoxFit.cover,
images: list,
onImageTap: (imageIndex) {
Navigator.of(context).push(
new MaterialPageRoute(
builder: (context) => new DishDetails(
detail_name:
snapshot.data[imageIndex].data["title"],
detail_picture: snapshot
.data[imageIndex].data["image"]),
),
);
},
autoplay: false,
dotSize: 4.0,
indicatorBgPadding: 4.0,
animationCurve: Curves.fastOutSlowIn,
animationDuration: Duration(milliseconds: 1000),
)));
When I hover over the function, I get the following error message:
The named parameter onImageTap isn't defined. Try correcting the name
to an existing named parameter or defining a new parameter with this
name
Can someone tell me how I can resolve this issue.
As you mentioned it was working earlier, so I believe you are using some 3rd party package which have Carousel widget and in one of its previous versions it had onImageTap, you unknowingly updated the plugin and they must have removed it or replaced it with something else.
Solution:
So, you can either use the previous version which had onImageTap property or use something else for it.
You may want to wrap your Carousel in a GestureDetector and handle onTap like:
GestureDetector(
onTap: () {},
child: Carousel(...)
)