Using the triple-shift (>>>) operator in Dart - flutter

Supposedly Dart 2.14 included the triple shift operator (>>>), but when I try to use it I get an error:
print(0xff >>> 1);
The compiler highlights the last > of the three and says:
Expected an identifier.
This is true on my local machine and in DartPad. Both are using the version of Dart 2.14 shipped with Flutter 2.5.
Am I doing something wrong or is this a problem with the release?

You can refer to my related answer here.
Any language features that are introduced in a given version require that version as the minimum constraint for your project. This means you need to update your pubspec.yaml:
environment:
sdk: '>=2.14.0 <3.0.0'
Now, you can use:
var foo = 42;
foo >>> 2;

If you compile this code, required Dart SDK version 2.14.1.
Dart language site’s documentation and examples use version 2.13.4 of the Dart SDK.
https://dart.dev/tools/sdk.
So when you are trying with Flutter, It uses SDK version less than 2.14.1.
Download the latest Flutter SDK
https://flutter.dev/docs/get-started/install/windows and replace
the old one. Update your pubspec.yaml file
environment:
sdk: ">=2.14.0 <3.0.0"
To run your code without Flutter app:
Download latest Dart SDK from https://dart.dev/tools/sdk/archive
Create a project with Android Studio IDE.
Create a new Flutter project.
Select Dart from the left menu.
Locate Dart SDK path, that you downloaded(Must be extracted)
Provide your project name and location.
Create a Dart file and write these lines of code.
void main() {
final value = 0x22;
print("Current value : $value");
print("Unsigned shift right value is : ${(value >>> 4)}");
}
Output is
F:/Dart/dart-sdk/bin/dart.exe --enable-asserts F:\Flutter\TestDartLanguage\main.dart
Current value : 34
Unsigned shift right value is : 2
Process finished with exit code 0
Enjoy happy coding. Thanks

Related

Bitrise flutter build keeps using older versions of flutter/dart

I have a flutter app that I am trying to build through bitrise.
I keep getting a tonne of errors along the lines of
Error: The 'super-parameters' language feature is disabled for this library.
Try removing the package language version or setting the language version to 2.17 or higher.
super.key
and it is blocking me from analyzing and building the app.
Now the issue is, I am using flutter install with upgrade set to true , and have sdk target in my pubspec.yaml as
Environment:
sdk: ">=2.17.6 <3.0.0"
I disabled the caching steps, and tried to create a new deployment pipeline, to no avail.
In your workflow, add Flutter Install, Then set your desired flutter version on Input Variables sections's Flutter SDK git repository version field. e.g. 2.8.0.

Error when reading 'migrate': No such file or directory

I want to use migration tool as described here.
But I get Error: Error when reading 'migrate': No such file or directory
Here my dart version and error
xxx#xxx-:~/Desktop/xxx$ dart --version
Dart VM version: 2.4.0 (Unknown timestamp) on "linux_x64"
xxx#xxx-I:~/Desktop/xxx$ dart migrate
Error: Error when reading 'migrate': No such file or directory
pubspec.yaml
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
**
First solution :**
dart --version
If the dart sdk version is less than 2.12.0
Then try:
Flutter upgrade --force
or
dart upgrade
If dart migrate still doesn'tt work then jump to the second solution
Second solution :
since dart is already downloaded as part of flutter
Make sure first that it hasn't been downloaded twice
on Mac try :
where dart
on MS windows , check your path in the environment variables.
If there was more than one path for the dart sdk , then this is problem
In my case I had it installed twice
/usr/local/bin/dart
/Users/USER/Documents/flutter/bin/dart
so I removed the first one by (mac os)
rm /usr/local/bin/dart
The moment I did that the issue was fixed.
Try
dart --version
again and you should be having the latest version of dart that comes with flutter and you should be able to run succsfully
dart migrate

Migrating to Flutter 2 fail

I have a project that is developed with Flutter v1.22.6 and was working great, after updating the flutter to v2.0.0 and reading the documentation, I've found out I can use dart migrate --apply-changes to fix issues that are related to flutter upgrade, By running the command I get dozens of errors and the error ends with:
The migration tool didn't start, due to analysis errors.
The following steps might fix your problem:
1. Set the lower SDK constraint (in pubspec.yaml) to a version before 2.12.
2. Run `dart pub get`.
3. Try running `dart migrate` again.
What is the simplest way to migrate the current project to the v2?
The problem is that you have already migrated to a newer version of dart.
You can't migrate your projects from the new version, there is literally nothing to migrate to.
If you want to migrate your older projects (which are without null safety) to a new version, your dart version must be older than 2.12.0 version.
In this case, consider changing the version in your pubspec.yaml file.
environment:
sdk: '>=2.11.0 <3.0.0'
Then run the command in your terminal
> dart migrate --apply-changes
make sure you have following step by step migrate dart 2
and base on your error notification try to set in pubspec.yaml
environment:
# Works in Dart 2 only.
sdk: '>=2.0.0 <3.0.0'

Dart 2.6 and Flutter, Upgrading Dependency

Using Flutter, I am trying to update my dart dependency 2.6.0 but I get an error
Because *project_name* requires SDK version >=2.6.0 <3.0.0, version solving failed.
pub get failed (1)
exit code 1
I want to use the new features that dart offers, such as Extension Methods. How could I go about upgrading my dependency? I have installed the newest stable version of dart on my computer, but regardless I still get that error.
A couple of things that might help:
From this issue, you can try ensuring that you've updated enviroment in your pubspec.yaml file like so:
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
Another thing that might help is just double checking the steps in this post or on the dart installation page to ensure that you've updated to the latest version correctly.

How to upgrade packages in VSCode/flutter?

I'm using VS Code on a flutter project. I just edited pubspec.yaml to point to a later version of a package, and it automatically ran 'flutter packages get'. In my '/development//flutter/.pub-cache/hosted/pub.dartlang.org' directory, I can see both versions. But when I compile, it looks like it's still using the old version. I tried various things like 'flutter packages upgrade', 'flutter clean', etc., but to no avail. Looking at the 2 package versions' source code, I can see the change I want in the newer version. How do I point to the new package? Thanks.
Update:
It's the 'ethereum' package that's not updating. I had used the 3.0.0 version (method expects 2 args), and then switched to the 3.1.0 version (method expects 3 args). But compiling with a 3 arg call balks with incorrect argument count:
client.admin.personalSendTransaction(BigInt.parse(currentAddress), currentPassword,{});
[dart] Too many positional arguments: 2 expected, but 3 found. [extra_positional_arguments_could_be_named]
Yet hovering over the method call does show it expects 3 args:
personalSendTransaction(BigInt address, String passphrase, {BigInt to, BigInt data, int gas, int gasPrice, int value, int nonce, int condition, bool conditionIsTimestamp: false}) → Future<BigInt>
pubspec.yaml:
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: ^0.1.2
# Get package(s) for talking to ethereum node
# web3dart: '>=0.3.0'
ethereum: ^3.1.0
# read barcodes and QR codes
barcode_scan: ^0.0.3
# Generate a QR code
qr: ^1.0.1
# Display as actual symbol
qr_flutter: ^1.1.5
dev_dependencies:
flutter_test:
sdk: flutter
In pubspec.lock:
ethereum:
dependency: "direct main"
description:
name: ethereum
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
Version 3.1.0 is what I want to be used.
Running 'flutter packages upgrade resolved' yields:
[Gregorys-iMac]:(gkd) ~/Programs/wine_track $ flutter packages upgrade resolved
Running "flutter packages upgrade" in .... 2.7s
In the package cache, I have both:
/Users/gkd/development//flutter/.pub-cache/hosted/pub.dartlang.org/ethereum-3.0.0/lib/src/api/ethereum_api_admin.dart
/Users/gkd/development//flutter/.pub-cache/hosted/pub.dartlang.org/ethereum-3.1.0/lib/src/api/ethereum_api_admin.dart
flutter pub upgrade --major-versions
you can simply run flutter packages upgrade in your project to upgrade all the packages. this feature is available from flutter version 1.17
Just use
flutter pub upgrade --major-versions
or
flutter packages upgrade
i have created a python3 script for this, you can use. this script only produce latest packages names and versions, you must to copy and paste into pubspec.yaml file.
import yaml
import requests
from lxml import etree
from io import StringIO
def getNewVersion(pkg_name):
url = f'https://pub.dev/packages/{pkg_name}'
with requests.get(url) as req:
doc = etree.parse(StringIO(req.text), etree.HTMLParser()).getroot()
title = doc.xpath('//h2[#class="title"]')[0].text.strip()
return '^' + (title.split(' ')[1])
if __name__ == "__main__":
filename = 'pubspec.yaml'
new_map = None
with open(filename, 'r') as _f:
docs = yaml.load(_f, Loader=yaml.FullLoader)
deps = docs['dependencies']
for package_name, old_version in deps.items():
if package_name == 'flutter':
continue
last_version = getNewVersion(package_name)
print(f'{package_name}: {last_version}')
OK, never mind. This was my mistake. I interpreted the declaration's "{ type:variablename, . . .}" as a map/hash. In reality, they're Dart's optional named parameters. Just using any of them without the wrapping braces compiles clean.
Go to https://pub.dartlang.org/packages and find the latest package from there.
In there you can see an install tab. click that.
Now you can add the dependencies into your pubspec.ymal file in your flutter project.
Then you can just press ctrl+s in VS Code or type flutter packages get in your terminal.
Now you can import into any page.