getbykey 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 54

Get by Key #

pub package

A simple extension to get an element in a list by it's key or id.

Why #

This package replaces

users.firstWhere((user) => user.id == myUuid, orElse: () => null)

with

users.getByKey(myUuid)

in your code.

Usage #

To use this plugin, add getbykey as a dependency in your pubspec.yaml file.

Then add import 'package:getbykey/getbykey.dart'; to your file to use the extension method.

Also you need to add a $key getter to the classes you wish to use in a list with this extension.

Example #

import 'package:getbykey/getbykey.dart';

class User {
  String id;
  String name;

  User({this.id, this.name});

  String get $key => id; // Add the `$key` getter to your class. In this example class the `id` is the key

  @override
  String toString() => 'User{name: $name}';
}

var users = <User>[
  User(
    id: '4b614020-1f3a-4f44-9435-5f834f8f4f90',
    name: 'Alice',
  ),
  User(
    id: 'eb40e648-d88f-45f3-bac0-26c8c392f4d9',
    name: 'Bob',
  ),
];

void main() {
  var user1 = users.getByKey('eb40e648-d88f-45f3-bac0-26c8c392f4d9');

  print(user1); // => User{name: Bob}

  var user2 = users.getByKey('this-key-does-not-exist');

  print(user2); // => null

  var user3 = users.getByKey('4b614020-1f3a-4f44-9435-5f834f8f4f90');

  print(user3); // => User{name: Alice}
}

1.0.1 #

  • Formatting

1.0.0 #

  • Initial version

example/getbykey_example.dart

import 'package:getbykey/getbykey.dart';

class User {
  String id;
  String name;

  User({this.id, this.name});

  String get $key =>
      id; // Add the `$key` getter to your class. In this example class the `id` is the key

  @override
  String toString() => 'User{name: $name}';
}

var users = <User>[
  User(
    id: '4b614020-1f3a-4f44-9435-5f834f8f4f90',
    name: 'Alice',
  ),
  User(
    id: 'eb40e648-d88f-45f3-bac0-26c8c392f4d9',
    name: 'Bob',
  ),
];

void main() {
  var user1 = users.getByKey('eb40e648-d88f-45f3-bac0-26c8c392f4d9');

  print(user1); // => User{name: Bob}

  var user2 = users.getByKey('this-key-does-not-exist');

  print(user2); // => null

  var user3 = users.getByKey('4b614020-1f3a-4f44-9435-5f834f8f4f90');

  print(user3); // => User{name: Alice}
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  getbykey: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:getbykey/getbykey.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
9
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
54
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Health issues and suggestions

Document public APIs. (-1 points)

3 out of 3 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0