我正在使用通过Json文件进行的本地化,并且可以正常工作,但是当我路由到特定页面时会出现错误,这是完整的页面代码,它可能有助于理解问题
与此同时,除此页面外,所有应用本地化均正常 heeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelp
lowing NoSuchMethodError was thrown building ProductDetails(dirty, dependencies:
I/flutter ( 5656): [_LocalizationsScope-[GlobalKey#af16d]], state: _ProductDetailsState#86477):
I/flutter ( 5656): The method 'translate' was called on null.
I/flutter ( 5656): Receiver: null
I/flutter ( 5656): Tried calling: translate("Buy Now")
I/flutter ( 5656):
I/flutter ( 5656): Widget creation tracking is currently disabled. Enabling it enables improved error messages. It can
I/flutter ( 5656): be enabled by passing `--track-widget-creation` to `flutter run` or `flutter test`.
I/flutter ( 5656):
I/flutter ( 5656): When the exception was thrown, this was the stack:
I/flutter ( 5656): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter ( 5656): #1 _ProductDetailsState.build (/pages/product_details.dart:254:64)
I/flutter ( 5656): #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4047:27)
-I/flutter ( 5656): #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3941:15)
I/flutter ( 5656): #4 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)
I/flutter ( 5656): #5 StatefulElement.update (package:flutter/src/widgets/framework.dart:4120:5)
I/flutter ( 5656): #6 Supplementation (package:flutter/src/widgets/framework.dart:2893:15)
I/flutter ( 5656): #7 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5134:14)
I/flutter ( 5656): #8 Supplementation (package:flutter/src/widgets/framework.dart:2893:15)
I/flutter ( 5656): #9 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16)
I/flutter ( 5656): #10 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)
Code:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import '/service/app_localizations.dart';
class ProductDetails extends StatefulWidget {
final prod_details_name;
final prod_details_picture;
final prod_details_old_price;
final prod_details_new_price;
ProductDetails(
{this.prod_details_name,
this.prod_details_new_price,
this.prod_details_old_price,
this.prod_details_picture});
@override
_ProductDetailsState createState() => _ProductDetailsState();
}
class _ProductDetailsState extends State<ProductDetails> {
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: [
Locale('en', 'US'),
Locale('ar', 'SA'),
],
localizationsDelegates: [
// THIS CLASS WILL BE ADDED LATER
// A class which loads the translations from JSON files
AppLocalizations.delegate,
// Built-in localization of basic text for Material widgets
GlobalMaterialLocalizations.delegate,
// Built-in localization for text direction LTR/RTL
GlobalWidgetsLocalizations.delegate,
],
// Returns a locale which will be used by the app
localeResolutionCallback: (locale, supportedLocales) {
// Check if the current device locale is supported
for (var supportedLocale in supportedLocales) {
if (supportedLocale.languageCode == locale.languageCode &&
supportedLocale.countryCode == locale.countryCode) {
return supportedLocale;
}
}
// If the locale of the device is not supported, use the first one
// from the list (English, in this case).
return supportedLocales.first;
},
home: Scaffold(
appBar: AppBar(
elevation: 0.1,
title: Text('${widget.prod_details_name}'),
// centerTitle: true,
backgroundColor: Colors.pink[900],
actions: <Widget>[
new IconButton(
icon: Icon(
Icons.search,
color: Colors.white,
),
onPressed: () {},
),
new IconButton(
icon: Icon(
Icons.shopping_cart,
color: Colors.white,
),
onPressed: () {},
)
],
),
body: new ListView(
children: <Widget>[
new Container(
height: 300.0,
child: GridTile(
child: Container(
color: Colors.white,
child: Image.network(widget.prod_details_picture),
),
footer: new Container(
color: Colors.white,
child: ListTile(
leading: new Text(
widget.prod_details_name,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.0,
color: Colors.pink[900]),
),
title: new Row(
children: <Widget>[
Expanded(
child: new Text(
' \$${widget.prod_details_old_price}',
style: TextStyle(
color: Colors.grey,
decoration: TextDecoration.lineThrough),
),
),
Expanded(
child: new Text('\$${widget.prod_details_new_price}',
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold)),
),
],
),
),
),
),
),
////////////////////First Button ////////////////////
// Row(
// children: <Widget>[
// /////////// Size Button ///////////
// Expanded(
// child: MaterialButton(
// onPressed: () {
// showDialog(
// context: context,
// builder: (context) {
// return new AlertDialog(
// title: new Text(
// AppLocalizations.of(context).translate("Size")),
// content: new Text(AppLocalizations.of(context).translate("Choose the Size")),
// actions: <Widget>[
// new MaterialButton(
// onPressed: () {
// Navigator.of(context).pop(context);
// },
// child : Text(AppLocalizations.of(context).translate("Size")),
// )
// ],
// );
// });
// },
// color: Colors.white,
// textColor: Colors.grey,
// elevation: 0.2,
// child: new Row(
// children: <Widget>[
// Expanded(
// child: new Text(
// AppLocalizations.of(context).translate("Close")),
// ),
// Expanded(
// child: new Icon(Icons.arrow_drop_down),
// ),
// ],
// ),
// ),
// ),
// /////////// Color Button ///////////
// Expanded(
// child: MaterialButton(
// onPressed: () {
// showDialog(
// context: context,
// builder: (context) {
// return new AlertDialog(
// title: new Text(AppLocalizations.of(context).translate("Color")),
`enter code here` // content: new Text(AppLocalizations.of(context).translate("Choose the Color")),
// actions: <Widget>[
// new MaterialButton(
// onPressed: () {
// Navigator.of(context).pop(context);
// },
// child: Text(
// AppLocalizations.of(context).translate("close")),
// )
// ],
// );
// });
// },
// color: Colors.white,
// textColor: Colors.grey,
// elevation: 0.2,
// child: new Row(
// children: <Widget>[
// Expanded(
// child: new Text('',)
// // AppLocalizations.of(context).translate("Color")),
// ),
// Expanded(
// child: new Icon(Icons.arrow_drop_down),
// ),
// ],
// ),
// ),
// ),
// /////////// Qty Button ///////////
// Expanded(
// child: MaterialButton(
// onPressed: () {
// showDialog(
// context: context,
// builder: (context) {
// return new AlertDialog(
// title: new Text(AppLocalizations.of(context).translate("Quantity")),
// content: new Text(AppLocalizations.of(context).translate("Choose the Quantity")),
// actions: <Widget>[
// new MaterialButton(
// onPressed: () {
// Navigator.of(context).pop(context);
// },
// child: Text(Professionalization(context).translate("Close")),
// )
// ],
// );
// });
// },
// color: Colors.white,
// text Color: Colors.grey,
// elevation: 0.2,
// child: new Row(
// children: <Widget>[
// Expanded(
// child: new Text('')
// // Professionalization(context).translate("Qty")),
// ),
// Expanded(
// child: new Icon(Icons.arrow_drop_down),
// ),
// ],
// ),
// ),
// ),
// ],
// ),
////////////////////Second Button ////////////////////
Row(
children: <Widget>[
/////////// buy Button ///////////
Expanded(
child: Material Button(
on Pressed: () {},
color: Colors.pink[900],
text Color: Colors.white,
elevation: 0.2,
child: new Text(Professionalization(context).translate("Buy Now"))
),
),
//////////// Icon Buttons ////////////
new Icon Button(
icon: Icon(Icons.add_shopping_cart),
on Pressed: () {},
color: Colors.pink[900],
),
new Icon Button(
icon: Icon(Icons.favorite_border),
on Pressed: () {},
color: Colors.pink[900],
),
],
),
Divider(),
new List Tile(
title: new Text(
Professionalization(context).translate("Product Details"),
style: Text Style(
font Weight: Counterweight,
font Size: 16.0,
color: Colors.pink[900])),
subtitle: Text(
Professionalization(context).translate("aaaqqqwwweee")),
),
Divider(),
new Row(
children: <Widget>[
Padding(padding: const EdgeInsets.fromLTRB(12.0, 5.0, 5.0, 5.0),
child: Text("",
// Professionalization(context).translate("Product Name :"),
style: Text Style(color: Colors.pink[900]),),
),
Padding(padding: const EdgeInsets.all(5.0),
child: Text(widget.prod_details_name, style: Text Style(color: Colors.grey),),)
],
),
new Row(
children: <Widget>[
Padding(padding: const EdgeInsets.fromLTRB(12.0, 5.0, 5.0, 5.0),
child: Text("",
// Professionalization(context).translate("Product Old Price :"),
style: Text Style(color: Colors.pink[900]),),
),
Padding(padding: const EdgeInsets.all(5.0),
child: Text('\$${widget.prod_details_old_price}', style: Text Style(color: Colors.grey, decoration: TextDecoration.lineThrough),),)
],
),
new Row(
children: <Widget>[
Padding(padding: const EdgeInsets.fromLTRB(12.0, 5.0, 5.0, 5.0),
child: Text("",
// Professionalization(context).translate("Product New Price :"),
style: Text Style(color: Colors.pink[900]),),
),
Padding(padding: const EdgeInsets.all(5.0),
child: Text('\$${widget.prod_details_new_price}', style: Text Style(color: Colors.red),),)
],
),
],
),
));
}
}