Git Product home page Git Product logo

dart-basics's Introduction

Dart CI pub package package publisher

This repository contains a collection of useful extension methods on the built-in objects in Dart, such as String, Iterable, and Object.

Usage

Import the basics library.

import 'package:basics/basics.dart';

Then use the methods directly on objects in your dart code.

import 'package:basics/basics.dart';

main() async {
  const numbers = <int>[2, 4, 8];

  if (numbers.all((n) => n.isEven)) {
    print('All numbers are even.');
  }

  print('sum of numbers is: ${numbers.sum()}');

  for (var _ in 5.range) {
    print('waiting 500 milliseconds...');
    await Future.delayed(500.milliseconds);
  }
}

Notes

This is not an official Google project.

dart-basics's People

Contributors

arielmagbanua avatar dependabot[bot] avatar devoncarew avatar eikob avatar jamesderlin avatar johnsonmh avatar kevmoo avatar pennzht avatar zdg2102 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dart-basics's Issues

version solving failed

I created a new project and used the basic: ^0.4.0 and with flutter run got below result / error

Because every version of flutter_test from sdk depends on quiver 2.0.5 and every version of basics depends on quiver ^2.1.2+1, flutter_test from sdk is incompatible with basics.
So, because DemoApp depends on both basics ^0.4.0 and flutter_test any from sdk, version solving failed.

If I comment this dependency and do flutter run, it works perfectly.

also below is output for flutter doctor -v

flutter doctor -v
[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Mac OS X 10.15.4 19E287, locale en-GB)
    • Flutter version 1.12.13+hotfix.9 at /Library/flutter
    • Framework revision f139b11009 (5 weeks ago), 2020-03-30 13:57:30 -0700
    • Engine revision af51afceb8
    • Dart version 2.7.2

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/dragonar/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.4, Build version 11E146
    • CocoaPods version 1.9.1

[✓] Android Studio (version 3.6)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 45.1.1
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.44.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.10.1

[✓] Connected device (1 available)
    • iPhone 11 Pro Max • 2BERFC15-950E-48GB-B556-F6797HH49DEF • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-4 (simulator)

• No issues found!

Add whereKey, whereValue, whereKeyValue on Iterable<MapEntry>

Dealing with MapEntrys is cumbersome:

final differentChunks = _newChunks.entries
        .where((entry) => entry.value.isDirty)
        .where((entry) => entry.value.data != _originalChunks[entry.key])
        .toList();

Extension methods for whereKey, whereValue and whereKeyValue on Iterable<MapEntry<K, V>> could make this much more readable:

final differentChunks = _newChunks.entries
        .whereValue((chunk) => chunk.isDirty)
        .whereKeyValue((index, chunk) => chunk.data != _originalChunks[index])
        .toList();

Code context: I have a List of data chunks and a List of possibly changed data chunks, which have an isDirty property to indicate whether some values were changed. It's possible that some data is changed and then changed back again, so to get a List of only those chunks that are actually different than the other, I first filter by the isDirty property and only then do the expensive comparison of actual values.

Question on changes on done as part of null safety release

Before the null safety release, the package relied on quiver for some APIs. For example isBlank was would call https://pub.dev/documentation/quiver/latest/quiver.strings/isBlank.html. This allowed passing a nullable string as isBlank would return true if it was null or only contained whitespace characters. With this PR from @zdg2102 to migrate the null safety this API other similar ones no longer accept nullable strings. This mean previously when developers were able to write

if(someString.isBlank) {
...
}

they now need to write

if(someString?.isBlank ?? true) {
...
}

I wanted to ask if this is an intentional change as this makes the library less useful in dealing with scenarios like this. Seems like something that perhaps should've been documented in the changelog as well but there's no mention of this. No unit tests exist for this API to have caught this change too

[NNBD] Rethink MapBasics.get

When I added MapBasics.get, I didn't give significant thought to NNBD. Now that I have, it's become clear that it will be broken, and it's unclear what we should do about it:

  • We could make MapBasics.get return V?, just like Map.operator []. This is the easiest, but it seems weirdly non-optimal since I made MapBasics.get take an optional default value:

    final map = <String, int>{};
    int x = map.get('foo', 0); // Would fail to compile.
    

    and people would have to use map.get(...)!.

  • We could make MapBasics.get return V. I think that would prevent the default value from ever being optional, even if V is nullable. That's a bit less convenient but probably would be acceptable. However, it also would prevent using map.get(key, null) if V is non-nullable, forcing people to use map[key] instead.

  • If Dart supported overloading, we could have the best of both worlds by doing V? get(K key) and V get(K key, V defaultValue). Alas. The next best thing would be to split MapBasics.get into two separately named methods. (I don't yet have any name suggestions.)

  • I am not an expert on the nuances of Dart generics, but possibly MapBasics.get could be generic with a parameterized return type. Ideally it'd be deducible from the union of V and the type of the default value (which either should be V or V?), but I am not optimistic that there is any magic workaround.

Get range from Iterable

It would be great to be able to get a range from a (possibly infinite) Iterable, similar to List's sublist:

var a = Iterable.generate(100, (i) => i).range(5, 20);
extension LazyRange<T> on Iterable<T> {
  Iterable<T> range(int from, int to) => skip(from).take(to - from);
}

Null safety

Is the null-safe version going to be published soon?

Make minBy more efficient

I currently have code like this:

final optimalFoo = foos.minBy((foo) {
  print('Calculating error score of $foo.');
  return expensiveErrorScoreComputation(foo);
});

And for a list of 5 foos, it prints this:

Calculating error score of Foo0.
Calculating error score of Foo1.
Calculating error score of Foo1.
Calculating error score of Foo2.
Calculating error score of Foo1.
Calculating error score of Foo3.
Calculating error score of Foo1.
Calculating error score of Foo4.

The score of the candidate is re-computed again and again. That's because the implementation of minBy is pretty naively implemented. I think it should be more optimized.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.