简体   繁体   English

如何从另一个 dart 文件访问值?

[英]How to access values from another dart file?

I am new to flutter.Here I stored a value to a variable doc_id ,I want to use this value in another file called comments.dart.我是 flutter 的新手。这里我将一个值存储到变量doc_id中,我想在另一个名为 comments.dart 的文件中使用该值。 So I did something like below but it gives null value in comment.dart.所以我做了类似下面的事情,但它在 comment.dart 中给出了 null 值。

await FirebaseFirestore.instance
    .collection('blogs')
    .add({
    'title': titleController.text,                         
}).then((value) {
  doc_id = value.id;

  comment(postid: docid);

  successAlert(context);
}).catchError((error) => 
 errorAlert(context));

Comment.dart评论.dart

class comment extends StatefulWidget {
  final String? postid;
  const comment({Key? key, this.postid}) : super(key: key);
  _commentState createState() => _commentState();
}

class _commentState extends State<comment> {
  @override
  Widget build(BuildContext context) {
    return        
        Text(widget.postid);
  } 
}

Just create a global variable and assign from there只需创建一个全局变量并从那里分配

String postid = "";

class comment extends StatefulWidget {
  final String? postid;
  const comment({Key? key, this.postid}) : super(key: key);
  _commentState createState() => _commentState();
}

class _commentState extends State<comment> {
  @override
  Widget build(BuildContext context) {
    return        
        Text(postid);
  } 
}

void setPostID(String s) {  // get value
   postid = s;
}

Finally assign the value最后赋值

await FirebaseFirestore.instance
    .collection('blogs')
    .add({
    'title': titleController.text,                         
}).then((value) {
  doc_id = value.id;
  
  setPostID(value.id);   // set value

  comment(postid: docid);

  successAlert(context);
}).catchError((error) => 
 errorAlert(context));
You can use: https://pub.dev/packages/shared_preferences

await FirebaseFirestore.instance
    .collection('blogs')
    .add({
    'title': titleController.text,                         
}).then((value) {
  doc_id = value.id;
  
  await prefs.setString('doc_id', postid);   // set value

  comment(postid: docid);

  successAlert(context);
}).catchError((error) => 
 errorAlert(context));

Finally use it in your class最后在你的 class 中使用它

class comment extends StatefulWidget {
  final String? postid;
  const comment({Key? key, this.postid}) : super(key: key);
  _commentState createState() => _commentState();
}

class _commentState extends State<comment> {

  @override
  void initState() {    
    super.initState();
    widget.postid = prefs.getString('doc_id');  // get the value
    setState(() {});
  }
  @override
  Widget build(BuildContext context) {
    return        
        Text(postid);
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从不同的 dart 文件访问变量? - How to access variables from different dart files? 如何使 Dart 在全球范围内检测来自不同文件的枚举扩展名? - How to make Dart detects enum extensions from different file globally? 如何使用 Dart 访问数据库中的值? - How to access a value in the database using Dart? 如何访问不在树中构建小部件的 dart 文件中的继承小部件? - How do I access my inherited widget in a dart file that doesn't build a widget in the tree? 如何在服务器端访问 dart 中的 firestore - How to access firestore in dart on server side 从 Flutter/Dart 访问受 AWS S3 保护的图像? - Access AWS S3 protected Images from Flutter/Dart? 如何将 If 语句添加到 main.dart 文件 - How Add a If statement to main.dart file 如何从 AzureML 文件共享访问本地文件? - How to access local files from AzureML File Share? 如何从 firebase 获取数据并将其存储到列表中以供在 dart 中进一步操作 - How to get data from the firebase and store it into a list for futher manipulation in dart 如何从一列中找到前 3 个返回值中的每一个,从另一列中查找前 3 个返回值? - How to find for each of the top 3 returned value from one column, the 3 top values from another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM