Mobile Examples

Complete code examples for Flutter mobile integration.

Updated 27 December 2024
mobileflutterexamplescode

Complete Flutter Examples

When autoFetch: true, Onairos automatically fetches user data after consent:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class OnairosApp extends StatefulWidget {
  @override
  _OnairosAppState createState() => _OnairosAppState();
}

class _OnairosAppState extends State<OnairosApp> {
  final Map<String, dynamic> requestData = {
    "Small": {
      "type": "Personality",
      "descriptions": "Insight into your Interests",
      "reward": "10% Discount",
    },
    "Medium": {
      "type": "Personality",
      "descriptions": "Deeper personality analysis",
      "reward": "2 USDC",
    },
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Onairos Demo')),
      body: Center(
        child: OnairosButtonWrapper1(
          webpageName: 'Mobile App',
          requestData: requestData,
          returnLink: 'yourapp://returnlink',
          autoFetch: true,
          onResolved: onResolved,
          inferenceData: {},
          proofMode: false,
          textColor: 'black',
          textLayout: 'right',
        ),
      ),
    );
  }
}

Manual API Call (AutoFetch Disabled)

When autoFetch: false, you handle the API call manually:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class OnairosApp extends StatefulWidget {
  @override
  _OnairosAppState createState() => _OnairosAppState();
}

class _OnairosAppState extends State<OnairosApp> {
  // Define the callback to handle API response
  Future<void> onResolved(String apiUrl, String accessToken) async {
    try {
      final response = await http.post(
        Uri.parse(apiUrl),
        headers: {
          "Authorization": "Bearer $accessToken",
          "Content-Type": "application/json",
        },
        body: jsonEncode(requestData),
      );

      if (response.statusCode == 200) {
        final Map<String, dynamic> data = jsonDecode(response.body);
        // Process Onairos Data
        print("Data: $data");
        
        // Update your UI with the user's personality data
        setState(() {
          // userTraits = data['Traits'];
        });
      } else {
        print("Error: ${response.statusCode}");
      }
    } catch (e) {
      print("Exception: $e");
    }
  }

  final Map<String, dynamic> requestData = {
    "Small": {
      "type": "Personality",
      "descriptions": "Insight into your Interests",
      "reward": "10% Discount",
    },
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Onairos Demo')),
      body: Center(
        child: OnairosButtonWrapper1(
          webpageName: 'Mobile App',
          requestData: requestData,
          returnLink: 'yourapp://returnlink',
          autoFetch: false,  // Manual mode
          onResolved: onResolved,
          inferenceData: {},
          proofMode: false,
          textColor: 'black',
          textLayout: 'right',
        ),
      ),
    );
  }
}
That's it!

You now have full access to user personality data. Check out the Inference Response documentation to understand the data format.