Flutter, AmplifyStorageS3 plugin not added correctly - flutter

getting
Error :
/*
dynamic AmplifyStorageS3()
package:my_budget_app/main.dart
AmplifyStorageS3 isn't a type.
Try correcting the name to match an existing type
*/
I am following this docs
https://docs.amplify.aws/lib/storage/getting-started/q/platform/flutter/#provision-backend-storage
Amplify storage S3 is not imported correctly.
I cleared cache and reinstalled all the pubs
I deleted publock file and pub get all the files no luck
I restarted vscode a couple of times no luck
I see the import says not used so AmplifyStorageS3 class is not exported from the package
ackage:amplify_storage_s3/amplify_storage_s3.dart ???

I have 3 ideas.
1: In package import import 'package:amplify_storage_s3/amplify_storage_s3.dart'; add "as ampl" to the end and then you will have something like this:
import 'package:amplify_storage_s3/amplify_storage_s3.dart' as ampl;
And then write:
ampl.AmplifyStorageS3 storage = ampl.AmplifyStorageS3();
2: Hold ctrl and left click on the imported package, find this class in the package and find out what's wrong.
3: Check your pubspec.yaml and so on for minimum requirements:
environment:
sdk: ">=2.12.0 <3.0.0"

Related

Internationalization in a monorepo

I have an app (just frontend) whose code is structured in a monorepo. There are separate packages for each custom widget. I would also like to have one package with all the translations. In this way, if another package need a translation I just need to import that package. So I created the translation package and in its main file I just wrote (here I use intl):
export 'package:flutter_gen/gen_l10n/app_localizations.dart';
Now, if I import that package in another package and I import the file with:
import 'package:l10n/l10n.dart';
It tells me that that import is not used and gives me error when I use the translation with AppLocalizations.of(context)!.foo
If you use VScode (don't know for other editors) you have to manualy import the generated translation package:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
It seems that it's not possible to export generated file by design (check out this github issue). The best solution that I've found to fix my problem is the following:
Create a translation package inside your project, like in packages/translations and add that package to the pubspec.yaml of your your main project. Like this:
dependencies:
flutter:
sdk: flutter
translations:
path: packages/translations
In that package create a l10n.yaml file like this:
arb-dir: lib
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
If you are working in a git repo, add packages/translations/lib/app_localizations*.dart in your .gitignore
Now if you run flutter packages get && flutter gen-l10n in that translations packages all the translations will be automatically generated (assuming they are in packages/translations/lib/app_xx.arb)
Now you can import the translation file from you main package with import 'package:translations/app_localizations.dart';
The only drawback is that the translations are not automatically generated when you type flutter pub get (see this issue). In order to regenerate them you have to type flutter gen-l10n inside the translations package every time. The solution could be improved by using a tool for managing Dart monorepo projects like melos.
An example implementation of what I described (including melos) could be found in this github repo

Error: Could not resolve the package 'app' in 'package:app/app_controller.dart

In my last flutter project, I included files placed in my lib/ directory by using import 'package:app/file_name.dart'.
For example, my file located at lib/app_controller.dart was imported via:
import 'package:app/app_controller.dart'
I just started a new flutter project, and it is giving me the error:
Error: Could not resolve the package 'app' in 'package:app/app_controller.dart'
When I remove the portion package:app/, it builds fine. I find this very strange because my previous project is still building just fine without any changes. Does anyone know what's happening here?
What is allowing my old project to respect package:app/..., but not my new project?
package:app/ would work only for an application that is called app. What is the name of your new application? When you import items from your own project, it goes like this:
package:{{YOUR APPLICATION NAME}}/{{DIRECTORIES}}
What is the name in your pubspec.yaml file? It's usually on the very first line.
it is because a dart class can be imported in two ways(AFAIK),
local import from the root of current file where import is being used for example import '../folder/file.dart
with a package name which should begin from package for example import package:packagename/any_file_in_the_lib_folder.dart, a package will have a pubspec.yml which defines a package name which will be used to import the content of the lib folder of that package, in your case your first project is named app so it respected this import style but your second project isn't respecting it because its not named app but something else.

flutter package can not find classes in package

I am trying to create a flutter package.
Inside Lib(package) folder, I have MY-PACKAGE.dart file. I also have a folder Called src which contains some codes I will import for use in MY-PACKAGE.dart.
In my pubspec.yaml in Example folder I have added the package like below and run packages get.
onboardly:
path: ../
To use my package I do this
// THERE IS NOT ANY PROBLEM WITH THIS IMPORT STATEMENT
import 'package:onboardly/onboardly.dart';
OnBoardly( // THIS CLASS WORKS FINE
screens: [
OnBoardlyScreenItem( // THIS CLASS CAN NOT BE FOUND EVEN THO IT EXISTS IN THE src FOLDER
image: Image.asset("assets/loadicon.png"),
description: Text("Hello There"),
),
],
),
The problem am facing is the fact that OnBoardlyScreenItem() which is in the src of the package can not be found.
I have ran
flutter packages get
flutter pub get
restarted my IDE
run flutter clean
Adding this for anyone later,
I had to export those classes or files that I wanted to be available to the Package to the "Entry file" since the folders in my case are regarded as private. Export all files or classes you'll want to be available to the user.
// Exporting all codes to be avaible to package
export 'package:onboardly/src/IntroScreen/OnBoardlyScreenItem.dart';

Flutter NFC reader plugin does not work with error inside plugin

I'm trying to make a flutter version of my old app , it needs NFC .
First I create a default project for test run and it works fine.
I tried to use flutter-nfc-reader and install it following "Installation" in that default project.
And here comes the problem:
After I edit "pubspec.yaml" and do "packeges get" , it automatically edit "GeneratedPluginRegistrant" under "myapp/android/app/src/main/java/io.flutter.plugins".
added
import it.matteocrippa.flutternfcreader.FlutterNfcReaderPlugin;
and
FlutterNfcReaderPlugin.registerWith(registry.registrarFor("it.matteocrippa.flutternfcreader.FlutterNfcReaderPlugin"));
But the import can't find that package "it.matteocrippa.flutternfcreader" which is inside the plugin.
I tried just copy that package but that file will be full of error when it is inside my project.
I have no idea how to fix it....
As Github reads:
last step import to the project:
import 'package:flutter_nfc_reader/flutter_nfc_reader.dart';

Target URI doesn't exist, when trying to import a package in flutter

I have stumbled upon a problem in importing the package in Flutter, I tried to solve this by running flutter packages get and also shutting down the project in Android studio and reopening it.
import 'package:task_02_category_widget/category.dart';
Here is the line above, and the error I'm running into when I run it gives the following error in the console.
Your application could not be compiled, because its dependencies could
not be established.
The following Dart file:
/Users/username/Documents/flutter_rectangle_2/lib/main.dart
...refers, in an import, to the following library:
package:task_02_category_widget/category.dart
That library is in a package that is not known. Maybe you forgot to
mention it in your pubspec.yaml file?
If task_02_category_widget/category.dart is part of an old project you are reusing you should put it in a folder in your flutter application and include it like "../ folder /task_02_category_widget/category.dart ". If it is part of github repository you have copy pasted from, just copy the file and use the step above. Most probably you are looking for that . In any other case check here to find the source code.
You should have in your project at a file called pubspec.yaml a definition like this:
name: my_app
dependencies:
task_02_category_widget:
Let’s say that your package is laid out as follows:
task_02_category_widget/
lib/
category.dart
Then, you can import it:
import 'package:task_02_category_widget/category.dart';
More information:
https://www.dartlang.org/tools/pub/get-started
https://www.dartlang.org/guides/libraries/create-library-packages