json_dynamic_widget_plugin_expressions

DISCONTINUED: the functionality provided by this plugin has been added to the base json_dynamic_widget package as of 4.0.0. This plugin is no longer needed and will no longer be maintained.

Table of Contents

Live Example

Introduction

Plugin to the JSON Dynamic Widget to provide expressions support utilizing the fantastic expressions package.

Using the Plugin

import 'package:json_dynamic_widget/json_dynamic_widget.dart';
import 'package:json_dynamic_widget_plugin_expressions/json_dynamic_widget_plugin_expressions.dart';


void main() {
  // Ensure Flutter's binding is complete
  WidgetsFlutterBinding.ensureInitialized();

  // ...

  // Get an instance of the registry
  var registry = JsonWidgetRegistry.instance;

  // Bind the plugin to the registry.  This is necessary for the registry to
  // find the function provided by the plugin
  JsonExpressionsPlugin.bind(registry);

  // ...
}

Once you have bound the plugin, you the expression function will be exposed. When using the expression function, you need to fully use the syntax from the expressions. All variables and functions from the registry will be exposed to that syntax, but it differs from the syntax of the functions and variables from JsonDynamicWidget.

For instance, you can do the following...

registry.setValue('x', 2);
registry.setValue('y', 4);
registry.registerFunction(
  'add',
  ({args, required registry}) => args![0] + args[1],
);

And then the result of this JSON:

{
  "type": "text",
  "args": {
    "text": "##expression(add(x+y))##
  }
}

... will result in 6 being printed out.