castle_flutter 1.1.3 copy "castle_flutter: ^1.1.3" to clipboard
castle_flutter: ^1.1.3 copied to clipboard

outdated

Castle Flutter plugin.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:castle_flutter/castle.dart';

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

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

class _MyAppState extends State<MyApp> {
  String _token = '';
  String _userId = '';
  String _baseUrl = '';
  int _queueSize = 0;
  String _userAgent = '';
  String _clientIdHeaderName = '';
  String _requestTokenHeaderName = '';

  @override
  void initState() {
    super.initState();
    initCastle();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initCastle() async {
    var token, userId, baseUrl, queueSize, userAgent, clientIdHeaderName, requestTokenHeaderName;
    try {
      await Castle.configure(
        publishableKey: "pk_SE5aTeotKZpDEn8kurzBYquRZyy21fvZ",
        debugLoggingEnabled: true,
        maxQueueLimit: 100,
        flushLimit: 20,
        useCloudflareApp: false,
        baseURLAllowList: ["http://google.com"],
      );
      token = await Castle.createRequestToken;
      userId = await Castle.userId;
      baseUrl = await Castle.baseUrl;
      queueSize = await Castle.queueSize;
      userAgent = await Castle.userAgent;
      clientIdHeaderName = await Castle.clientIdHeaderName;
      requestTokenHeaderName = await Castle.requestTokenHeaderName;
    } on PlatformException {

    }

    // 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(() {
      _token = token.toString();
      _userId = userId.toString();
      _baseUrl = baseUrl.toString();
      _queueSize = queueSize;
      _userAgent = userAgent.toString();
      _clientIdHeaderName = clientIdHeaderName.toString();
      _requestTokenHeaderName = requestTokenHeaderName.toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Castle TestApp'),
        ),
        body: Container(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text(
                _token
              ),
              Text(
                _userId
              ),
              Text(
                _baseUrl
              ),
              Text(
                _queueSize.toString()
              ),
              Text(
                _userAgent
              ),
              Text(
                _clientIdHeaderName
              ),
              Text(
                  _requestTokenHeaderName
              ),
              new ElevatedButton(
                  onPressed: _identify,
                  child: new Text("Identify")
              ),
              new ElevatedButton(
                  onPressed: _trackScreen,
                  child: new Text("Track Screen")
              ),
              new ElevatedButton(
                  onPressed: _reset,
                  child: new Text("Reset")
              ),
              new ElevatedButton(
                  onPressed: _flush,
                  child: new Text("Flush Events")
              ),
            ],
          ),
        )
      ),
    );
  }

  void _identify() {
    Castle.identify("thisisatestuser1");
  }

  void _trackScreen() {
    Castle.screen("Example screen");
  }

  void _reset() {
    Castle.reset();
  }

  void _flush() {
    Castle.flush();
  }
}
copied to clipboard
3
likes
0
points
645
downloads

Publisher

unverified uploader

Weekly Downloads

2024.05.30 - 2025.04.24

Castle Flutter plugin.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on castle_flutter