Shalabyer
Shalabyer

Reputation: 635

How to use dynamic strings with flutter_localizations

I use these two classes for localization in flutter following this article setup.

import 'package:flutter/cupertino.dart';

abstract class Languages {
  final String? extra;

  Languages({this.extra});
  static Languages? of(BuildContext context, {String? extra}) {
    return Localizations.of<Languages>(context, Languages);
  }
String get text199;
}

and this LanguageEn:

import 'languages.dart';

class LanguageEn extends Languages
{
  LanguageEn({String? extra}) : super(extra: extra);
  @override
String get text199 => "Update data($extra)";
}

when I pass this extra parameter I get null "Update data(null)" in whatever language I'm in

Languages.of(context,extra: "$activeID")!.text199),

but when I directly select a language class and do this it works and I get the number and not null "Update data(4)"

LanguageEn(extra: "$activeID").text199),

how to pass dynamic strings with this package?

Upvotes: 0

Views: 64

Answers (1)

Shalabyer
Shalabyer

Reputation: 635

Solved it by replacing that Language class with the following:

import 'package:flutter/cupertino.dart';

abstract class Languages {
    String? extra;

  Languages({this.extra});
  static Languages? of(BuildContext context, {String? extra}) {
    final Languages? instance = Localizations.of<Languages>(context, Languages);
    if (instance != null) {
      instance.extra = extra; // Assign the extra parameter to the instance.
    }
    return instance;
  }

  String get text199;
  }

Upvotes: 0

Related Questions