AI Chat in Flutter: Architecture and Key Safety

By Darus S Alfatah · 2025-09-16 · Tutorial · 8 menit

AI Chat in Flutter: Architecture and Key Safety

Author note: We keep the key on the backend, not in the Flutter app, so it cannot be extracted from the package.

The One Rule That Matters

Do not embed an API key in a Flutter app. APK and IPA files can be unpacked, and strings inside them are readable. Anything shipped to a device should be treated as public, so the key must live on a server you control.

The standard pattern is a small proxy endpoint that authenticates the user, applies quotas, and forwards requests. Your app then holds only a user session token.

Layering the App

Separate three concerns: UI widgets, a chat state controller, and a service that performs network calls. When you later add caching or switch providers, only the service changes.

Keep message models immutable and store them in one place. Scattered state is the main reason chat screens become unmaintainable after a few iterations.

Streaming Responses

Streaming makes the app feel responsive: text appears as it arrives instead of after a long pause. Use an HTTP client that exposes a byte stream, append chunks to state, and auto-scroll while respecting user scroll position.

Provide a stop button. Users should be able to cancel a long answer, which also saves tokens.

Poor Connectivity

Mobile networks drop. Show clear states: sending, waiting, streaming, done, failed. Queue messages locally and retry on reconnect, but avoid duplicating a request that may have succeeded silently.

Cache recent conversations so the app opens instantly even offline, and mark unsent messages visibly.

Accessibility and UX

Support large text, screen readers, and high contrast. Chat interfaces often rely on color alone to indicate status, which excludes some users.

Keep the input area fixed above the keyboard and avoid layouts that jump when the answer arrives.

Frequently Asked Questions

Can I use Firebase to hide the key? Not reliably; secrets in config files are extractable.

Should I cache responses? Yes, for repeated questions.

How do I limit usage per user? Enforce quotas server-side.

What about push notifications? Useful for long background jobs.

How do I test streaming? Use a simulator with network throttling enabled.

Conclusion

A Flutter AI app is mostly architecture and state handling. Keep secrets on the server, stream responses, and design for bad networks.

Testing and Release

Test on a real device with throttled network, not only on a simulator with fast Wi-Fi. Streaming bugs, timeouts, and keyboard overlap almost always appear on physical hardware first.

Before release, verify that the app never logs full prompts in production builds, that error messages tell users what to do next, and that a cancelled request actually stops. These three checks prevent most support tickets in the first weeks.

After release, watch crash reports and average response times by device model. Older devices reveal performance problems that new hardware hides.

Designing the First Screen

Show a short list of suggested prompts instead of an empty input. New users rarely know what to ask, and suggestions set expectations about what the assistant can do.

Keep the history accessible and let users delete conversations. Storage you do not need becomes a liability, both technically and legally.

An example backend proxy: The Flutter app sends a message to your API, the API holds the key and calls the gateway, then returns the answer. The key is never on the device.

Frequently Asked Questions

Can beginners follow this tutorial?

Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.

Is BankAI’s endpoint compatible with the library I already use?

Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.

Where do I get an API key to follow this tutorial?

You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.

Sources & References

About the author

Darus S Alfatah — Founder & Writer

Darus S Alfatah is the founder of BankAI. He builds and operates the LLM API gateway that serves Indonesian developers, and writes about LLM integration, gateway architecture, and using AI practically and cost-effectively. Every guide is tested in a real environment — including code, configuration, and token calculations — before it is published, and updated when provider documentation or pricing changes.

Editorial policy

Related articles

🚀 Ready to Use the All-in-One API Key?

Sign up, top up with QRIS, and start building with ChatGPT, Claude, DeepSeek and every other model through one key.

🔑 SIGN UP NOW · Pricing