Flutter 通过最终参数

Flutter 通过最终参数,flutter,dart,Flutter,Dart,我目前正在开发一个flifter项目,以便从互联网上刮取一篇文章。我有点麻烦,因为我试图从youtube(下面的链接)复制一个家伙,但在UI中使用我的风格 我想复制这些步骤,用他的步骤来刮网站,但我遇到了这样一个小麻烦: home page class ArticlePage扩展StatefulWidget{ @凌驾 State createState()=>ArticlePageState(); 最后的字符串名; 第页(此名称); } 类ArticlePageState扩展状态{ @凌驾

我目前正在开发一个flifter项目,以便从互联网上刮取一篇文章。我有点麻烦,因为我试图从youtube(下面的链接)复制一个家伙,但在UI中使用我的风格

我想复制这些步骤,用他的步骤来刮网站,但我遇到了这样一个小麻烦:

home page

class ArticlePage扩展StatefulWidget{
@凌驾
State createState()=>ArticlePageState();
最后的字符串名;
第页(此名称);
}
类ArticlePageState扩展状态{
@凌驾
void initState(){
super.initState();
getdata();
}
getdata()异步{
var url='1〕https://www.***.com/artikel/${widget.name}/';
var response=wait http.get(url);
documentdocument=parser.parse(response.body);
final main class=document.getElementsByClassName('sc-htpNat egaha');
打印(主类);
}
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
返回脚手架(
正文:SingleChildScrollView(
物理:物理(),
子:列(
儿童:[
文本(${widget.name}引号),样式:TextStyle(fontSize:20,fontWeight:fontWeight.w600)
],
),
)
);
}
}
问题在于主页上的正文有一个错误

“无法使用静态访问访问实例成员'name'”


问题正是你所说的


ArticlePage.name
是一个静态引用。没有涉及任何实例。相反,您应该使用字符串,因为这是实例字段“name”的类型。将ArticlePage.name替换为“Article name”ArticlePage.name“我不知道您到底想要什么”这段代码。只需传递字符串类型参数。ex)‘物品’
import 'package:artickle/pages/ArticlePage.dart';
import 'package:artickle/pages/FavoritesPage.dart';
import 'package:artickle/pages/HistoryPage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget{
  @override
  State createState() => HomePageState();
}

class HomePageState extends State<HomePage> with TickerProviderStateMixin {

  TabController _tabController;

  @override
  void initState(){
    super.initState();
    _tabController = new TabController(initialIndex: 1, vsync: this, length: 3);

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          title: new Text('Artickle'),
          elevation: 0.7,
          actions: <Widget>[
          IconButton(
              icon: new Icon(Icons.search),
              color: Colors.white,
              onPressed: (){},
              ),
            ],
            bottom: new TabBar(
              controller: _tabController,
              indicatorColor: Colors.white,
              tabs: <Widget>[
                new Tab(child: new Text("Article", style: Theme.of(context).textTheme.button)),
                new Tab(child: new Text("Favorite", style: Theme.of(context).textTheme.button)),
                new Tab(child: new Text("History", style: Theme.of(context).textTheme.button)),
            ],
        ),
    ),
        body: new TabBarView(
            controller: _tabController ,
            children: <Widget>[
              new ArticlePage(ArticlePage.name), -> error
              new FavoritesPage(),
              new HistoryPage()
            ]
        ),
    );
  }
}
Article page
class ArticlePage extends StatefulWidget{

  @override
  State createState() => ArticlePageState();
  final String name;
  ArticlePage(this.name);
}

class ArticlePageState extends State<ArticlePage>{

  @override
  void initState() {
  super.initState();
  getdata();
  }

  getdata()async{
    var url = 'https://www.***.com/artikel/${widget.name}/';
    var response = await http.get(url);
    dom.Document document = parser.parse(response.body);
    final mainclass = document.getElementsByClassName('sc-htpNat eGAHHA');
    print(mainclass);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: SingleChildScrollView(
        physics: ScrollPhysics(),
        child: Column(
          children: <Widget>[
            Text("${widget.name} quotes", style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),)
          ],
        ),

      )
    );

  }


}