Flutter

Integrating Eartho into Your Flutter App

This section describes how to integrate Eartho into a Flutter app.

1. Install the SDK

You can install the SDK using the Flutter package manager.

Using Flutter package manager:

flutter pub add eartho_one

2. Retrieve Eartho Client ID, Client Secret, and Access ID

Go to Eartho Creator and copy your Eartho client ID, client secret, and access ID from the "Developers Integration" section.

3. Configure URL Scheme for iOS

  1. Open your Flutter project in Xcode.

  2. Go to the project settings and select your app target.

  3. Go to the Info tab and add a new URL type:

    • Identifier: eartho

    • URL Schemes: $(PRODUCT_BUNDLE_IDENTIFIER).one.eartho.world

Example:

Identifier: eartho
URL Schemes: $(PRODUCT_BUNDLE_IDENTIFIER).one.eartho.world

4. Initialize and Configure the SDK

Integrate Eartho into your Flutter app by configuring the SDK and handling authentication.

Example Code:

main.dart

Add the following code to initialize and handle authentication in your app.

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

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

class MyApp extends StatelessWidget {
  // Initialize Eartho
  final earthoOne = EarthoOne(
    clientId: 'YOUR_EARTHO_CLIENT_ID',
    clientSecret: 'YOUR_EARTHO_CLIENT_SECRET',
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Eartho Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Eartho Flutter Demo', earthoOne: earthoOne),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title, required this.earthoOne}) : super(key: key);

  final String title;
  final EarthoOne earthoOne;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  UserProfile? _user;
  bool _loading = true;

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

  void _initEartho() async {
    await widget.earthoOne.init();
    final user = await widget.earthoOne.getUser();
    setState(() {
      _user = user;
      _loading = false;
    });
  }

  void _login() async {
    try {
      await widget.earthoOne.connectWithRedirect('YOUR_EARTHO_ACCESS_ID');
      final user = await widget.earthoOne.getUser();
      setState(() {
        _user = user;
      });
    } catch (e) {
      print('Login failed: $e');
    }
  }

  void _logout() async {
    try {
      await widget.earthoOne.logout();
      setState(() {
        _user = null;
      });
    } catch (e) {
      print('Logout failed: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: _loading
            ? CircularProgressIndicator()
            : _user != null
                ? Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text('Hello, ${_user!.displayName}'),
                      SizedBox(height: 20),
                      ElevatedButton(
                        onPressed: _logout,
                        child: Text('Log out'),
                      ),
                    ],
                  )
                : ElevatedButton(
                    onPressed: _login,
                    child: Text('Log in'),
                  ),
      ),
    );
  }
}

Additional Resources

For more detailed examples and usage, visit the Eartho Flutter Example Repository.

Last updated