【发布时间】:2020-12-27 11:23:14
【问题描述】:
我目前正在开发一个 Flutter 项目来抓取互联网上的一篇文章。我遇到了一些麻烦,因为我试图从 youtube 复制一个人(下面的链接),但使用我的 UI 风格。
我想复制步骤以用他的步骤https://youtu.be/S4lB5Q9wM_s?t=546 抓取网站,但我遇到了这样的小麻烦:
home page
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),)
],
),
)
);
}
}
问题是首页的body有错误
“不能使用静态访问来访问实例成员 'name'”
【问题讨论】:
-
'ArticlePage.name' 我不知道你到底想要什么代码。只需传递字符串类型参数。例如)“文章”