Skip to content

Initialize

You will need to initialize your app via some credentials you can get from the console.

Hardcoding the credentials

import hopcolony

hopcolony.initialize(username="your-user-name", project="your-project", token="your-project-token")
import aiohopcolony
import asyncio

async def main():
    await aiohopcolony.initialize(username="your-user-name", project="your-project", token="your-project-token")

asyncio.run(main())

About the command asyncio.run(main())...

Whenever we want to use aiohopcolony, we need to do it inside a coroutine.

In this example, we create a new coroutine called main and run it using asyncio.

import 'package:hop_init/hop_init.dart' as init;

void main() async {
    await init.initialize(
        username: "your-user-name",
        project: "your-project",
        token: "your-project-token");

    runApp(MyApp());
}

Always run init.initialize before running the app...

Once the app starts with the call runApp, all the hopcolony instances you define will access the init package. Therefore, it is important that hop_init knows about the credentials before the app starts.

Using a file

The file you need to have the following syntax:

username: your-user-name
project: your-project
token: your-project-token

Place the config file with the name .hop.config in the directory where you are running the script:

import hopcolony

hopcolony.initialize()

You can also have the config file in another location and with another name and reference it running:

import hopcolony

hopcolony.initialize(config_file="~/hop-credentials/config")

Place the config file with the name .hop.config in the directory where you are running the script:

import aiohopcolony
import asyncio

async def main():
    await aiohopcolony.initialize()

asyncio.run(main())

You can also have the config file in another location and with another name and reference it running:

import aiohopcolony
import asyncio

async def main():
    await aiohopcolony.initialize(config_file="~/hop-credentials/config")

asyncio.run(main())

Place the config file with the name .hop.config in the root of your app (at the level of the pubspec.yaml). Then add this file to the assets of your appplication:

flutter:
    ...
    assets:
    - .hop.config

Then, you can just call init.initialize without any argument:

import 'package:hop_init/hop_init.dart' as init;

void main() async {
    await init.initialize();

    runApp(MyApp());
}

About waiting the initialize again...

Now it becomes more evident why we need to await the initialization before starting the app: we need to read a file from memory, which is an asynchronous operation.

Tip

Try to use the file method and add it to .gitignore if you don't want to expose the credentials in your git repo.