How do I run dart code that's isolated from my flutter project without creating a new dart project? - flutter

So I'm currently following a tutorial on flutter and it showed this nifty way of running dart code in the same project by creating the new file in the app directory instead of the lib folder. I'm using VS code and can't really figure out how to run the file (they used Android Studio). I tried running the file in the terminal and it kept saying
./scratch.dart: line 1: syntax error near unexpected token `('
./scratch.dart: line 1: `void main() {'
Any idea what I'm doing wrong?

Run dart in terminal to Check that you have dart installed globally with global environment variable in your terminal. If not follow this doc to install https://dart.dev/get-dart.
After making sure that you have dart SDK installed globally in your PC, you can run any dart file containing a void main function in a terminal like
~$ dart path_to_file/scratch.dart
Note: Running un terminal will not work if you are working with a package that utilize dart:ui

Related

After flutter new project creation I can't locate the main dart file to start programming

I have installed Android Studio and flutter successfully. When I run flutter doctor in command prompt it gives me [all green ticks in these brackets].
The question is that after the creation of a new flutter project on the right-hand side of the screen under the project I am unable to see/locate any dart main file to write code. For further clarification, you can see the attached image below.
Unable to locate the dart file to write code in the project hierarchy:
I have 0 ideas what is still missing in the configuration of Flutter / Android Studio
Well, I figured out why pubspec.yaml and main.dart was not in my project due to the older version of flutter. I downloaded the latest version flutter_windows_2.2.2-stable, created a new flutter project, and included the flutter SDK path of flutter_windows_2.2.2-stable which resolved all my problems. Kindly, make sure whoever wants to start Mobile Application development in Android Studio + Flutter + Dart always download the latest version of everything.
Recent Experiment:
After downloading the latest version of Flutter still, I was unable to get the pubspec.yaml and main.dart files in my Android Studio Project, so the solution is below:
where you have extracted your flutter_windows_2.2.2-stable remember the directory path like E:\src\flutter_windows_2.2.2-stable which is in my scenario.
******************* CMD WORK *******************
Open cmd(Command Prompt) by default you will be in the directory like C:\Users\UserName\
Now go to the path E:\src and to navigate write E: hit enter, now you will be in the E: directory. Write cd E:\src hit enter.
type flutter create project_name ( make sure whenever you withe the name of the project it should be like this my_first_app in this format you name the projects with proper naming convention in flutter [word_word_word_word] ) then hit enter, wait for a while it will create a new flutter project for you including pubspec.yaml and main.dart files + necessary files to be imported.
Reference for naming convention in flutter (https://medium.com/flutter-community/file-and-folder-structure-in-flutter-967b8be3155e#:~:text=Flutter%20doesn't%20have%20a,letters%20and%20underscores%20between%20words.&text=When%20naming%20folders%2C%20you%20want,them%20very%20direct%20and%20clear.)
Right now after the creation of a project(Flutter) in Android Studio you are unable to see/locate the pubspec.yaml and main.dart files use cmd for the project creation which worked in my scenario.

Error setting-up flutter_localizations package "Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart'."

This error appears when I try to add language support to my flutter application:
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist.
I’m using flutter_localizations, the project is built with dart 2.7.0
It seems that flutter_localizations needs latest sdk which is dart 2.13.0
How can I fix this?
I had this same issue with import 'package:flutter_gen/gen_l10n/app_localizations.dart' in Visual Studio Code after updating to Flutter 2.5.
After closing the VS Code window and reopening it, the error was gone.
If that doesn't work, try closing and opening VS Code again.
Or restart the Dart Analysis Server by going to View > Command Palette and then typing Dart: Restart Analysis Server.
Credit goes to https://stackoverflow.com/a/68150402/4797855 and https://stackoverflow.com/a/68731436/4797855.
Open Command Palette and select Dart: Restart Analysis Server.
Just type in the project terminal flutter gen-l10n it will solve your problem.
Also, in the pubspec.yaml file, enable the generate flag. This is added to the section of the pubspec that is specific to Flutter, and usually comes later in the pubspec file.
# The following section is specific to Flutter.
flutter:
generate: true # Add this line
just in Your terminal run this
flutter pup add flutter_gen/gen_l10n
It fixed my app
I had this issue before, and I tried to solve it by adding charts_flutter to my pubspec.yaml but it didn't solve my.
Lucky me, charting library recently released a new version which is compatible with flutter 2.5. I didn't need to add anything to my pubspec(I removed chart_flutter from my ). just run the app even if the compiler shows an error saying Target of URI doesn't exist for the import line. The system is going to generate that file on runtime.
Be sure that there's an l10n.yaml file in your project root as follows.
Example
Demonstration variables
foldername = l10n
defaultIntlFile = intl_en.arb
importFileName = app_localizations.dart
Import
import 'package:flutter_gen/gen_l10n/importFileName';
l10n.yaml
arb-dir: lib/foldername
template-arb-file: intl_en.arb
output-localization-file: app_localizations.dart
Structure
lib/foldername/defaultIntlFile
The solution in my case:
"If you are using a package flutter_gen you need to remove it from pubscpec.yaml to resolve conflict." credit Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'

Trying to generate proto to dart file

I'm trying to generated proto file to dart file with protoc-plugin follow this instruction https://grpc.io/docs/quickstart/dart/ but when I run this command line
protoc --dart_out=grpc:lib/proto --plugin=protoc-gen-dart=C:/src/flutter/flutter/.pub-cache/bin/protoc-gen-dart.bat -Iprotobuf protobuf/utils.proto
And it showed this error:
'dart' is not recognized as an internal or external command,
operable program or batch file.
'pub' is not recognized as an internal or external command,
operable program or batch file.
--dart_out: protoc-gen-dart: Plugin failed with status code 1.
I have installed dart as a plugin in my Android Studio. Is that I have to install dart SDK and add it to environment variable path to work?
Update
It worked after i install dart sdk and restart my pc.
Seeing the Flutter tag, I presume that Flutter SDK was what you had before you install Dart SDK as said in your update.
If you want to do this without having to install Dart SDK separately (since Flutter SDK has Dart SDK within itself already), the environment has to know where the following are at:
Flutter SDK
eg: export PATH="~/desktop/development/flutter/bin:$PATH"
Dart SDK within Flutter SDK
eg: export PATH="~/desktop/development/flutter/bin/cache/dart-sdk/bin:$PATH"
Pub cache within Flutter SDK (where the protoc plugin will be located at)
eg: export PATH="$PATH":"$HOME/desktop/development/flutter/.pub-cache/bin"
The given examples above are from .bash_profile in macOS, but in essence, the environment has to know where these 3 are before trying to globally activate the plugin via flutter pub global activate protoc_plugin and then generate Dart code from proto files.
The file protoc-gen-dart.bat actually has bugs in it. It's trying to run exe called pub which doesn't exist. You need to manually edit the file. It should be
dart pub global run protoc_plugin:protoc_plugin %*
not
pub global run protoc_plugin:protoc_plugin %*
Once I changed this, I was able to generate protos for Dart on Windows

new flutter project in vscode has error

when i create a new flutter project in vscode it will immediately shows an error in the main.dart file and gives a gradle error when trying to debug.
shown here: main.dart error
"compiler message: lib/main.dart:68:19: Error: Too few positional arguments: 1 required, 0 given."
I've reinstalled dart and flutter plugins. updated the java install.
in debug console i get this:
debug console
added output of flutter doctor -v
enter image description here
I was also facing the same error, but it was always on my test folder.
This is what I did:
On your terminal, make sure you are in your project repository.
Run the flutter pub get command, it worked for me, may be you
should try it too.
Do you possibly have two different versions of the Flutter SDK on your machine? I wonder if one is being used by flutter create and the other for analysis.
If you're sure you only have one, please open an issue on GitHub and attach a log file for me to investigate.

Flutter run says '...dependencies could not be established' for the meterial.dart library

I am new to flutter.io and trying to create my first flutter app according to the following guide
https://codelabs.developers.google.com/codelabs/first-flutter-app-pt1/#2
I used terminal command flutter run to start my application and then found an error as follows.
Your application could not be compiled, because its dependencies could not be established.
The following Dart file:
./startup_namer/lib/main.dart
...refers, in an import, to the following library:
/opt/flutter/packages/flutter/lib/meterial.dart
Unfortunately, that library does not appear to exist on your file system.
Couldn't find more details on how to resolve this issue.I am gussing that i should install this library package within my setup.What is the easiest method to install flutter library package from terminal?
Note: i created the app using command flutter create startup_namer
Judging by the error message you have mis-spelled the package in the import statements meterial.dart
It should be:
import 'package:flutter/material.dart';
Note: You may run into same error if you were to change the variable "name: myapp" in your pubspec.yaml file with "import 'package:myapp/settings.dart';" as the custom package in the main.dart file as was intended to add "comanyName" text to the app launcher icons. Fix: changed back to "name: myapp" removed the error