karte_core 0.0.1 copy "karte_core: ^0.0.1" to clipboard
karte_core: ^0.0.1 copied to clipboard

outdated

Flutter plugin for KARTE Core.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:karte_core/karte_core.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _visitorId = 'Unknown';
  bool _isOptOut = false;

  @override
  void initState() {
    super.initState();
    initPlatformState();
    Tracker.view("test");
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    print("initPlatformState");
    String visitorId;
    bool isOptOut;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      print("await");
      var results = await Future.wait([KarteApp.visitorId, KarteApp.isOptOut]);
      print("get results $results");
      visitorId = results[0];
      isOptOut = results[1];
    } on PlatformException {
      visitorId = 'Failed to get visitor id.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _visitorId = visitorId;
      _isOptOut = isOptOut;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('KARTE Core example app'),
        ),
        body: Center(
          child: Column(
            // mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Visitor id:  $_visitorId'),
              Text('isOptOut:  $_isOptOut'),
              RaisedButton(
                onPressed: () => Tracker.track("test"),
                child: Text("track"),
              ),
              RaisedButton(
                onPressed: () => Tracker.identify({"name": "sample"}),
                child: Text("identify"),
              ),
              RaisedButton(
                onPressed: () => Tracker.view("test"),
                child: Text("view"),
              ),
              RaisedButton(
                onPressed: () async {
                  KarteApp.optIn();
                  await initPlatformState();
                },
                child: Text("optIn"),
              ),
              RaisedButton(
                onPressed: () async {
                  KarteApp.optOut();
                  await initPlatformState();
                },
                child: Text("optOut"),
              ),
              RaisedButton(
                onPressed: () async {
                  var url = await UserSync.appendingQueryParameter("https://example.com");
                  print("url: $url");
                },
                child: Text("userSync"),
              ),
            ],
          ),
        )
      ),
    );
  }
}