messages_dart 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 60

Messages #

Install #

For Dart or Flutter #

messages_dart: any

Flutter Only #

messages_flutter: any

MessagesPlugin #

All functions is in MessagesPlugin.

Messages #

Messages has 3 implementation:

  • StringMessages - Pure text messages.
  • ErrorMessages - Throwable object and stack trace messages.
  • RoutingMessages - Routable messages.

StringMessage #

var msg = StringMessages("Pure text messages");

ErrorMessages #

void main(){
  try {
    //do something
  } catch (error, stack) {
    ErrorMessages.of(error?.toString(), stack);
  }
    
  Observable.just(data).listen(null, onError: (error, stack){
    ErrorMessages.of(error?.toString(), stack);
  });
}

RoutingMessages #

void main(){
  RoutingMessages.of("login");
  RoutingMessages.of("login", "buttons");
  RoutingMessages.of("login", "buttons", "accountHint");
  RoutingMessages.create([100, 6000, 900001]);
}

Usage #

Prepare Messages Resource File #

Messages Resource file supports any formats which can parse by json like json, yaml, xml or properties.

Base Resource Format

keydescription
messagedisplay message
remarkRemark message
routesChildren routes

Simple Content

{
    "message": "This is a default message",
    "routes": {
        "100": "Hello World!",
        "101": "Goodnight",
        "102": "Yes, I do!",
        "home": "Okay~",
        "login": "What the hell?"
    }
}

Message of routing [100] map to "Hello World!".

Message of routing [home] map to "Okay~".

Message of routing [600] map to "This is a default message".

Complex Content

{
    "message": "This is a default message",
    "routes": {
        "100": {
            "remark": "remark",
            "message": "Hello World!"        
        },
        "101": "Goodnight",
        "102": "Yes, I do!",
        "home": {
            "remark": "home page",
            "message": "Okay~"
        },
        "login": "What the hell?"
    }
}

Message of routing [100] map to "Hello World!".

Remark of routing [100] map to "remark".

Message of routing [home] map to "Okay~".

Remark of routing [home] map to "home page".

Nested Routing

{
    "message": "This is a default message",
    "routes": {
        "100": {
            "remark": "remark",
            "message": "Hello World!",
            "routes": {
                "5000": "Apple is red",
                "6000": "Banana is yellow"
            }  
        },
        "101": "Goodnight",
        "102": "Yes, I do!",
        "home": {
            "remark": "home page",
            "message": "Okay~"
        },
        "login": {
            "message": "What the hell?",
            "routes": {
                "buttons": {
                    "routes": {
                        "accountHint": "Please input account",
                        "passwordHint": {
                            "message": "Please input password"
                        }
                    }
                }
            }
        }
    }
}

Message of routing [100, 5000] map to "Apple is red".

Message of routing [100, 6000] map to "Banana is yellow".

Message of routing [100, 6000, 99000] map to "Banana is yellow".

Message of routing [100, 9000] map to "Hello World!".

Message of routing [login, buttons, accountHint] map to "Please input account".

Load Messages Resource File #

Raw Json

void main(){
  File resource = File("filepath");
  String json = resource.readAsStringSync();
  
  MessagesPlugin.loadAsString(json);
  // or
  JsonElement jsonElement = JsonElement.fromJsonString(json);
  MessagesPlugin.load(jsonElement);
}

Yaml

Add dependency to pubspec.yaml

dependencies:
  yaml: ^2.2.0
import 'dart:convert';
import 'package:yaml/yaml.dart' as yaml;

void main(){
  File resource = File("filepath");
  String content = resource.readAsStringSync();
  
  var doc = yaml.loadYaml(content);
  String json = json.encode(doc);
  
  MessagesPlugin.loadAsString(json);
  // or
  JsonElement jsonElement = JsonElement.fromJsonString(json);
  MessagesPlugin.load(jsonElement);
}

Xml

Add dependency to pubspec.yaml

dependencies:
  xml2json: ^4.1.1
import 'dart:convert';
import 'package:xml2json/xml2json.dart';

void main(){
  File resource = File("filepath");
  String content = resource.readAsStringSync();
  
  var xml2Json = Xml2Json();
  xml2Json.parse(content);
  String json = json.encode(xml2Json.toGData());
  
  MessagesPlugin.loadAsString(json);
  // or
  JsonElement jsonElement = JsonElement.fromJsonString(json);
  MessagesPlugin.load(jsonElement);
}

Throw and Handle Error #

void main(){
  MessagesPlugin.loadAsString(jsonEncode(msg));

  MessagesPlugin.setErrorHandler((Messages messages) {
    print("Custom Handle message: ${messages.message}");
  });
  MessagesPlugin.setErrorTransformer((error, [stack]) {
    if (error is int) return StringMessages("int: $error");
    return StringMessages(error?.toString());
  });

  Future.sync(() {
    // do something
    throw RoutingMessages.of(100, 5000);
  }).catchError(MessagesPlugin.errorHandler).then((_) {
    throw 100;
  }).catchError(MessagesPlugin.errorHandler).then((_){
    throw "An Error~";
  }).catchError(MessagesPlugin.errorHandler);
}

Get Message from Routing #

var msg = RoutingMessages(100, 5000).message;

Extensions #

Getting as RoutingMessages

void main(){
  // list extension for routing
  RoutingMessages routingMessages = [100, 5000].toRoutingMessages();
}

Routing Message

void main(){
  var message = [100, 5000].routingMessage;
  message = ["login", "buttons", "accountHint"].routingMessage;
}

Error Handler

void main(){
  Future.sync(() {
    throw 100;
  }).catchErrorDefault().then((_) {
    throw [100, 5000].toRoutingMessages();
  }).catchErrorDefault();
}

Display

void main() {
  MessagesPlugin.display("Hello World");
  MessagesPlugin.displayString("Hello World");
  MessagesPlugin.displayError("An Error");
  MessagesPlugin.displayRouting(100, 5000);
  
  // messages extension
  Messages messages = StringMessages("Hello World");
  messages.display();
  
  // list extension for routing
  [100, 5000].display();  
}

1.0.0 #

  • Initial version, created by Stagehand

1.0.1 #

  • List and Future extensions added

1.0.2 #

  • Update readme.md

1.0.3 #

  • Optimize codes

example/example.dart

import 'dart:convert';

import 'package:messages_dart/messages_dart.dart';
import 'package:xson/xson.dart';

var routingConfig = {
  "message": "This is a default message",
  "routes": {
    "100": {
      "remark": "remark",
      "message": "Hello World!",
      "routes": {
        "5000": "Apple is red",
        "6000": "Banana is yellow",
      }
    },
    "101": "Goodnight",
    "102": "Yes, I do!",
    "home": {
      "remark": "home page",
      "message": "Okay~",
    },
    "login": {
      "message": "What the hell?",
      "routes": {
        "buttons": {
          "routes": {
            "accountHint": "Please input account",
            "passwordHint": {
              "message": "Please input password",
            }
          }
        }
      }
    }
  }
};

main() async {
  /// load routing config
  /// you can use format json, yaml, xml or properties to load just converting to json
  var routingConfigJson = jsonEncode(routingConfig);
  // if load by string, string's format must be json
  MessagesPlugin.loadAsString(routingConfigJson);

  /// set messages display delegate
  /// ex:
  ///   print()
  ///   showToast()
  MessagesPlugin.setDelegate((m) async {
    print(m);
    return true;
  });

  /// create messages instance
  var m1 = StringMessages("Some message");
  try {} catch (error, stack) {
    var m2 = ErrorMessages.of(error, stack);
  }
  var m3 = RoutingMessages.of(100, 5000);
  var m3Equals = RoutingMessages.create([100, 5000]);
  var m4 = RoutingMessages.of("login", "buttons", "accountHint");
  var m4Equals = RoutingMessages.create(["login", "buttons", "accountHint"])..extras = "Extras Data";
  var m5 = [100, 5000].toRoutingMessages();

  /// messages members
  m3.message; // display message
  m4Equals.extras; // user extras data
  m4Equals.remark; // message remark in routing config
  [100, 5000].routingMessage; // list extension

  /// display message
  MessagesPlugin.display("message");
  MessagesPlugin.displayRouting(100, 5000);
  MessagesPlugin.displayString("mesage");
  m3.display();
  m4.display();
  [100, 5000].display(); // list extension

  /// error handling with messages
  MessagesPlugin.setErrorHandler((message) => message.display());
  MessagesPlugin.setErrorTransformer((error, [stack]) {
    if (error is CustomError) return StringMessages("A custom error");
    return StringMessages("Unhandled error");
  });
  Future.sync(() {
    // do something
    // throw a messages error
    throw [100, 5000].toRoutingMessages(); // list extension
  })
      .catchError((error, stack) {
        // catch the messages error and print target of routing message
        if (error is Messages) print("[Error] ${error.message}");
      })
      .then((_) {
        // do something
        // throw a messages error
        throw RoutingMessages.of(100, 5000);
      })
      .catchErrorDefault() // future extension, catching and handling error by messages error handler
      .then((_) {
        // do something
        // throw a not messages error, error transformer will convert it to messages
        throw CustomError();
      })
      .catchError(MessagesPlugin.errorHandler);
}

class CustomError {}

Use this package as a library

1. Depend on it

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


dependencies:
  messages_dart: ^1.0.3

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:messages_dart/messages_dart.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
60
Learn more about scoring.

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

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/messages_plugin.dart. (-0.50 points)

Analysis of lib/src/messages_plugin.dart reported 1 hint:

line 15 col 22: Use ; instead of {} for empty constructor bodies.

Format lib/src/messages_handler.dart.

Run dartfmt to format lib/src/messages_handler.dart.

Format lib/src/messages_mapper.dart.

Run dartfmt to format lib/src/messages_mapper.dart.

Format lib/src/messages_model.dart.

Run dartfmt to format lib/src/messages_model.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
meta ^1.1.8 1.1.8
xson ^1.0.4 1.0.4
Transitive dependencies
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.5
typed_data 1.1.6