jt_xapi_tools 1.0.5 copy "jt_xapi_tools: ^1.0.5" to clipboard
jt_xapi_tools: ^1.0.5 copied to clipboard

A toolkit for sending and querying xAPI statements with built-in relay support.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:jt_xapi_tools/xapi_connector_main.dart';

void main() {
  runApp(const MaterialApp(home: XAPITestPage()));
}

class XAPITestPage extends StatefulWidget {
  const XAPITestPage({super.key});

  @override
  State<XAPITestPage> createState() => _XAPITestPageState();
}

class _XAPITestPageState extends State<XAPITestPage> {
  final TextEditingController _endpointController = TextEditingController();
  final TextEditingController _keyController = TextEditingController();
  final TextEditingController _secretController = TextEditingController();

  String _responseBody = "No data yet";
  bool _isRelayMode = true;

  // This would be your actual Service/Interface from your toolkit
  dynamic getLRS() {
    if (_isRelayMode) {
      return RelayLRS(
        relayEndpoint: _endpointController.text,
        storeKey: _keyController.text,
      );
    } else {
      return ManualLearningRecordStore(
        endpoint: _endpointController.text,
        key: _keyController.text,
        secret: _secretController.text,
      );
    }
  }

  Future<void> sendTestStatement() async {
    setState(() => _responseBody = "Sending...");

    try {
      // Wrap your data in the actual Classes defined in your toolkit
      final testStatement = Statement(
        actor: Actor(
          mbox: "mailto:test@jtdev.tools",
          name: "Tester",
        ),
        verb: Verb(
            id: "http://adlnet.gov/expapi/verbs/completed",
            display: "completed"),
        object: Activity(
          id: "https://jtdev.tools/activities/test-page",
        ),
      );

      final res = await getLRS().sendStatement(testStatement);
      setState(() => _responseBody =
          "Success! Statement Sent.\nStatus: ${res.statusCode}");
    } catch (e) {
      setState(() => _responseBody = "Error: $e");
    }
  }

  Future<void> queryTestStatements() async {
    setState(() => _responseBody = "Querying...");
    try {
      // Calling with a Map is correct here based on your interface
      final res = await getLRS().queryStatements({'limit': '1'});

      if (res != null) {
        // res.body contains the JSON string returned by your PHP relay or LRS
        setState(() =>
            _responseBody = "Status: ${res.statusCode}\nBody: ${res.body}");
      } else {
        setState(() => _responseBody = "Error: Received no response from LRS.");
      }
    } catch (e) {
      setState(() => _responseBody = "Error: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("JT xAPI Toolkit Tester")),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            SwitchListTile(
              title: Text(_isRelayMode ? "Relay Mode" : "Direct LRS Mode"),
              value: _isRelayMode,
              onChanged: (val) => setState(() => _isRelayMode = val),
            ),
            TextField(
              controller: _endpointController,
              decoration: const InputDecoration(labelText: "Endpoint URL"),
            ),
            TextField(
              controller: _keyController,
              decoration: InputDecoration(
                labelText: _isRelayMode ? "Store Key" : "API Key",
              ),
            ),
            if (!_isRelayMode)
              TextField(
                controller: _secretController,
                decoration: const InputDecoration(labelText: "API Secret"),
              ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                  onPressed: sendTestStatement,
                  child: const Text("Send Statement"),
                ),
                ElevatedButton(
                  onPressed: queryTestStatements,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.blueGrey,
                  ),
                  child: const Text("Query LRS"),
                ),
              ],
            ),
            const Divider(height: 40),
            const Text(
              "Response:",
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            // Replace your existing response Container with this:
            Container(
              padding: const EdgeInsets.all(8),
              width: double.infinity,
              color: Colors.grey[200],
              child: SelectionArea(
                child: SelectableText(
                  _responseBody,
                  style: const TextStyle(
                      fontFamily:
                          'Courier'), // Makes code errors easier to read
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
0
points
317
downloads

Publisher

unverified uploader

Weekly Downloads

A toolkit for sending and querying xAPI statements with built-in relay support.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

cupertino_icons, flutter, http

More

Packages that depend on jt_xapi_tools