اتصال کاربر واردشده به خطاها
پیش از شروع
- Abrova Auth و ابرووا تریس در یک اپ
When a user writes "the app crashed", you want their errors, not everyone's. Abrova Trace takes a user id and puts it on every error, native crash and log line after that. Abrova Auth already knows the id, and its authStateChanges stream tells you every time it changes. One listener connects the two.
Trace needs only the user id, never the user's token, e-mail or name.
Start both SDKs#
Both packages come from Abrova Center (see the Trace Flutter SDK and Auth Flutter SDK for the Android and iOS repository lines):
# pubspec.yaml
dependencies:
abrova_trace_flutter_sdk:
hosted: https://center.abrova.ir/api/packages/abrova/pub
version: ^0.2.6
abrova_auth:
hosted: https://center.abrova.ir/api/packages/abrova/pub
version: ^0.1.1Start Trace first, with initWithZone, and Abrova Auth inside its callback. Both use the same public key:
import 'package:flutter/material.dart';
import 'package:abrova_trace_flutter_sdk/abrova_trace_flutter_sdk.dart';
import 'package:abrova_auth/abrova_auth.dart';
const abrovaApiKey = 'ab_live_your_api_key';
void main() async {
await AbrovaTrace.initWithZone(
AbrovaTraceConfig(apiKey: abrovaApiKey, release: '1.0.0'),
() async {
WidgetsFlutterBinding.ensureInitialized();
await AbrovaAuth.initialize(abrovaApiKey); // restores the saved session
followSignedInUser(); // step 2
runApp(const MyApp());
},
);
}Follow the user with one listener#
authStateChanges emits the current user first, then again on every sign-in, sign-out and profile change. Set the Trace user id each time:
import 'dart:async';
StreamSubscription<AbrovaAuthUser?>? _traceUserSub;
void followSignedInUser() {
_traceUserSub ??= AbrovaAuth.instance.authStateChanges.listen((user) {
if (user != null) {
AbrovaTrace.setUserId(user.id); // ru_…
} else {
AbrovaTrace.setUserId(''); // signed out: later errors carry no user id
}
});
}Because the stream starts with the current user, a user restored from the saved session is set at launch too, before anything can fail. Guests count: an anonymous user has an id like any other (see Sign in guests and upgrade them later), and when a guest links a sign-in method the id stays the same.
The Flutter Trace SDK has no separate call to clear the user, so an empty id stands for "nobody signed in".
Optional: say whether it is a guest#
Whether the user is still a guest often matters when you read an error. Add it as a tag in the same listener:
if (user != null) {
AbrovaTrace.setUserId(user.id);
AbrovaTrace.setTag('account', user.anonymous ? 'guest' : 'member');
}Keep it to the id and coarse facts like this. Don't send e-mail addresses, names or phone numbers to Trace unless you really need them.
Check that it worked#
- Sign in on a test device, then cause an error, for example with a button whose
onPressedis() => throw StateError('Abrova user test'). - In Abrova Console → Trace → Issues, open the issue. In the Context card, Extra data shows
user_idwith the Abrova Auth id, and the Breadcrumbs include User ID set. - In Abrova Auth › Users, look up that id: it is the user you signed in as.
An issue groups every repeat of the same error and keeps the extra data of the latest occurrence, so the issue shows the user it last happened to.
Crashes and logs carry it too#
You don't need more code for these:
- Native crashes on Android and iOS:
setUserIdpasses the id to the bundled native SDK, so the crash report sent on the next launch is tagged with the same user. - Logs you send with
AbrovaTrace.info(…)and the other level helpers after enabling logging carry the user id as well: open an entry in Trace → Logs and its details show the User ID.
Troubleshooting#
Errors at start have no user id
Call followSignedInUser() right after AbrovaAuth.initialize and before runApp. initialize restores the session without a network call, and the stream emits the restored user at once.
Errors after sign-out still show the previous user
The listener must handle null: call AbrovaTrace.setUserId('') when user is null. Check that the subscription is not cancelled when a screen closes; keep it for the life of the app.
The id in Trace doesn't match any user in Abrova Auth › Users
Another place in your app also calls AbrovaTrace.setUserId with your own id. Use one source of user ids, the Abrova Auth id, everywhere. Also check that you are looking at the same Abrova project in both pages.
A guest's errors show a new id after reinstalling
A guest's session lives only on the device that created it. A reinstall creates a new guest. Ask guests to link a sign-in method if they want to keep one account.
- ایمیل یا نام را فقط در صورت نیاز بفرستید.
بعدی: SDK فلاتر