admin管理员组文章数量:1201647
This happened after migrating from Flutter 3.10.5 to 3.24.5.
I have a method on a class that uses a String with the information of what FontWeight
to use. Basically, something like this:
final fontWightText = `w400`;
final fontWeight = FontWeight.values.firstWhere(
(element) => element
.toString()
.endsWith(fontWeightText),
),
The problem is that, while running the app on release mode, this throws a Bad state: No element
error. If I run on debug, it works fine.
I noticed that if I print FontWeight.values.first
on debug, I get FontWeight.w100
, while on release I get Instance of 'FontWeight'
.
I'm trying to find a workaround for this, but I also wonder if it might happen elsewhere on my app since it only happens if I run this new version of Flutter.
This happened after migrating from Flutter 3.10.5 to 3.24.5.
I have a method on a class that uses a String with the information of what FontWeight
to use. Basically, something like this:
final fontWightText = `w400`;
final fontWeight = FontWeight.values.firstWhere(
(element) => element
.toString()
.endsWith(fontWeightText),
),
The problem is that, while running the app on release mode, this throws a Bad state: No element
error. If I run on debug, it works fine.
I noticed that if I print FontWeight.values.first
on debug, I get FontWeight.w100
, while on release I get Instance of 'FontWeight'
.
I'm trying to find a workaround for this, but I also wonder if it might happen elsewhere on my app since it only happens if I run this new version of Flutter.
Share Improve this question asked Jan 21 at 16:47 Gabriel BertolloGabriel Bertollo 233 bronze badges 1 |2 Answers
Reset to default 2Interesting question!
toString()
will have a different output between debug vs release mode, like you said
It is by design to optimize and reduce a lot of extra information in output log
Basically logic code should not rely on this method since it is mostly just for logging
Hello and welcome to StackOverflow!
As was pointed out in the comments, the toString
implementation of FontWeight
returns only constants values, so probably the issue is somewhere in your configuration. Maybe you need to update flutter, clear caches or something like that: I'd start by adding some print
lines, so you can try do debug what's happening in release mode.
I scribbled up a MRE that you can run in a DartPad, so you can see that with the latest version of flutter it's running as supposed:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final fontWeightText = 'w400';
final fontWeight = FontWeight.values.firstWhere(
(element) {
print(element.hashCode() +' vs '+fontWeightText);
return element.toString().endsWith(fontWeightText);
},
);
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Column(
children: [
Text('Hello, World!'),
Text(
'Hello, World!',
style: TextStyle(fontWeight: fontWeight),
)
],
),
),
),
);
}
}
本文标签: debuggingFlutter toString() seems to be different on debug and release modeStack Overflow
版权声明:本文标题:debugging - Flutter toString() seems to be different on debug and release mode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738616511a2102938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
FontWeight.toString
shown in the Flutter 3.27.2 documentation, it does not seem to be one of them. – jamesdlin Commented Jan 21 at 17:33